zenlotus / argparse

Automatically exported from code.google.com/p/argparse
Other
0 stars 0 forks source link

parent parser args are positional #26

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
rrs@champaran:~/apt-offline  (master)$ ./apt-offline get /tmp/update.uris --
usage: apt-offline get [-h] [--socket-timeout 30] [-d apt-downloads] [-s .]
                       [--no-check] [-t 1] [--bundle apt-offline-bundle.zip]
                       [--bug-reports]
                       apt-offline.sig
apt-offline get: error: unrecognized arguments: --verbose
rrs@champaran:~/apt-offline  (master)$ ./apt-offline --verbose get
/tmp/update.uris --threads 5 --bundle /tmp/update.zip
rrs@champaran:~/apt-offline  (master)$ ./apt-offline -h
usage: apt-offline [-h] [-v] [--verbose] [--test-windows]
                   {gui,set,install,get} ...

Offline APT Package Manager

positional arguments:
  {gui,set,install,get}

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         show program's version number and exit
  --verbose             Enable verbose messages
  --test-windows        This switch is used while doing testing on windows.

(C) 2005 - 2009 Ritesh Raj Sarraf - This program comes with ABSOLUTELY
NO
WARRANTY. This is free software, and you are welcome to redistribute it
under
the GNU GPL License

What is the expected output? What do you see instead?
I would want to see main parsers args/options to be allowed to be placed
after the sub-parser args/options also.
In the above example, --verbose is a global option. argparse restricts me to
specify the option as the first argument to the main command. It would be
better if it allowed me to specify it anywhere.

What version of the product are you using? On what operating system?
1.0
Linux

Original issue reported on code.google.com by riteshra...@gmail.com on 2 Sep 2009 at 5:30

GoogleCodeExporter commented 9 years ago
I don't think that allowing the main parser options interspersed with the 
subparser
options is right for all applications. For example, while 'svn --version' 
works, 'svn
co --version' doesn't. So at least SVN has decided that --version is not a 
global option.

Right now, the only real way to use global options is to store all your global
options in one parser, and then add those options to all your subparsers using 
the
parser= keyword argument to the ArgumentParser constructor. Something like this:

>>> global_options = argparse.ArgumentParser(add_help=False)
>>> global_options.add_argument('--bar', action='store_true')
>>> parser = argparse.ArgumentParser(parents=[global_options])
>>> subparsers = parser.add_subparsers()
>>> parser1 = subparsers.add_parser('1', parents=[global_options])
>>> parser.parse_args(['--bar', '1'])
Namespace(bar=True)
>>> parser.parse_args(['1', '--bar'])
Namespace(bar=True)

I'm open to patches which make it easier to specify global options if you have
specific ideas.

Original comment by steven.b...@gmail.com on 8 Sep 2009 at 1:11

GoogleCodeExporter commented 9 years ago

Original comment by steven.b...@gmail.com on 8 Sep 2009 at 1:11

GoogleCodeExporter commented 9 years ago
s/using the parser= keyword/using the parents= keyword/

Original comment by 4kir4...@gmail.com on 10 Sep 2009 at 3:41

GoogleCodeExporter commented 9 years ago
Closing this as the parents keyword allows the desired behavior to be requested 
explicitly. If you'd like to propose an API for this, please either discuss on 
the argarse users list, or file a feature request on bugs.python.org.

Original comment by steven.b...@gmail.com on 23 Jul 2010 at 10:53