Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
Nested subparsers?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
MaxB  
View profile  
 More options Jun 11 2011, 8:09 am
From: MaxB <max.benen...@gmail.com>
Date: Sat, 11 Jun 2011 05:09:56 -0700 (PDT)
Local: Sat, Jun 11 2011 8:09 am
Subject: Nested subparsers?
Hi Steven,

How can I use the argparse in this way:

>> prog option1 {only cmd1 and cmd2 are available}
>> prog option2 {only cmd3 and cmd4 are available}

NOTE: each cmd{#} defined as a subparser with its unique set of
parameters.

It's close to the behavior of "svn commit" for example with an
addition of a mandatory
parameter before any subparser, this parameter determines which
subparser to call, say:
"svn local commit" is legal but if "remote" is used instead of "local"
the "commit" option
won't be available at all and the user may use "svn remote copy" for
example.

I tried to define nested subparsers but got: "cannot have multiple
subparser arguments".
May be I'm missing something but currently I'm unable to reach the
desired behavior.

TIA,
MaxB.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven Bethard  
View profile  
 More options Jun 14 2011, 5:48 am
From: Steven Bethard <steven.beth...@gmail.com>
Date: Tue, 14 Jun 2011 11:48:12 +0200
Local: Tues, Jun 14 2011 5:48 am
Subject: Re: Nested subparsers?

On Sat, Jun 11, 2011 at 2:09 PM, MaxB <max.benen...@gmail.com> wrote:
> How can I use the argparse in this way:
>>> prog option1 {only cmd1 and cmd2 are available}
>>> prog option2 {only cmd3 and cmd4 are available}

Perhaps I don't understand the question, but I would just call
add_subparsers() on the main parser, and then again call
add_subparsers() on each of the child parsers you create, e.g.:

main_parser = argparse.ArgumentParser()
main_subparsers = main_parser.add_subparsers()
option1_parser = main_subparsers.add_parser('option1')
option1_subparsers = option1_parser.add_subparsers()
option1_subparsers.add_parser('cmd1')
option1_subparsers.add_parser('cmd2')
option2_parser = main_subparsers.add_parser('option2')
option2_subparsers = option2_parser.add_subparsers()
option2_subparsers.add_parser('cmd3')
option2_subparsers.add_parser('cmd4')
main_parser.parse_args()

Steve
--
Where did you get that preposterous hypothesis?
Did Steve tell you that?
        --- The Hiphopopotamus


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »