Argparse -- add optional arguments in help string - python

I have some script on python and argparse, one of optional arguments adds transliteration:
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--text',
action='store_true',
help='display a text')
parser.add_argument('-s', '--search',
dest='string',
action='store',
type=str,
help='search in a text')
parser.add_argument('--translit',
action='store_true',
help='transliterate output; usage: prog [-t | -d STRING] --translit')
results = parser.parse_args()
if len(sys.argv) == 1:
parser.print_help()
elif results.text and results.translit::
translit(display_text())
elif results.text and results.translit::
display_text()
elif results.string and results.translit:
translit(search(results.string))
elif results.string:
search(results.string)
Output:
usage: prog [-h] [-t] [-s STRING] [--translit]
optional arguments:
-h, --help show this help message and exit
-t, --text display a text
-s STRING, --search STRING search in a text
--translit transliterate output; usage: prog
[-t | -s STRING] --translit
There is no output, when I run prog --translit. I need the string looking so:
usage: prog [-h] [-t] [-s STRING] [[-t | -s STRING] --translit]
When I run prog --translit, the string of output should be:
prog: error: argument --translit: usage: [[-t | -s STRING] --translit]
How can I do this?

You could make translit a subparser/subcommand which would work as prog translit -h or just prog translit which if there are needed options missing, would display the help text.
Something along the lines of
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--string', required=True)
subparsers = parser.add_subparsers(dest='subcommand_name')
translit_parser = subparsers.add_parser('translit')
translit_parser.add_argument('x', required=True)
parser.parse_args()
python test.py translit would output something like:
usage: test.py translit [-h] -x X
test.py translit: error: argument -x is required

You probably should write a separate condition for that since there is none that is exclusively checking for that:
elif results.translit:
parser.print_help()

Related

Argparse to show usage help for subcommand

Consider this arg_test.py script:
#!/usr/bin/env python3
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help='subcommand help')
# create the parser for subcommand
parser_a = subparsers.add_parser('do-stuff', help='Help for do-stuff subcommand')
args = parser.parse_args()
With the help for the subcommand I get:
./arg_test.py do-stuff -h
usage: arg_test.py do-stuff [-h]
optional arguments:
-h, --help show this help message and exit
How can I add some usage description when using -h? For example I would like to get:
./arg_test.py do-stuff -h
usage: arg_test.py do-stuff [-h]
This subcommand does awsome stuff << HOW DO I ADD THIS?
optional arguments:
-h, --help show this help message and exit

collecting input files from command line in unix using argparse library

I'm trying to write a script that would take some flags and files as arguments and then execute other scripts, depend on the flag that the user chooses. For example, the command line should look like that:
main_script.py -flag1 -file_for_flag_1 another_file_for_flag_1
and
main_script.py -flag2 -file_for_flag_2
I tried to use the argparse library, but I don't know how to take the input files as arguments for the next steps and manipulate them as I want. I started with:
parser = argparse.ArgumentParser(description="Processing inputs")
parser.add_argument(
"-flat_map",
type=str,
nargs="+",
help="generates a flat addressmap from the given files",
)
parser.add_argument(
"-json_convert",
type=str,
nargs="+",
help="generates a flat addressmap from the given files",
)
args = parser.parse_args(args=["-flat_map"])
print(args)
I printed args in the end to see what I get from it but I got nothing I can work with.
Would like to have some guidance. Thanks.
You can convert the args to a dict (where the key is the arg option and the value is the arg value) if it's more convenient for you:
args_dict = {key: value for key, value in vars(parser.parse_args()).items() if value}
Using argparse you can use sub-commands to select sub-modules:
import argparse
def run_command(parser, args):
if args.command == 'command1':
# add code for command1 here
print(args)
elif args.command == 'command2':
# add code for command2 here
print(args)
parser = argparse.ArgumentParser(
prog='PROG',
epilog="See '<command> --help' to read about a specific sub-command."
)
subparsers = parser.add_subparsers(dest='command', help='Sub-commands')
A_parser = subparsers.add_parser('command1', help='Command 1')
A_parser.add_argument("--foo")
A_parser.add_argument('--bar')
A_parser.set_defaults(func=run_command)
B_parser = subparsers.add_parser('command2', help='Command 2')
B_parser.add_argument('--bar')
B_parser.add_argument('--baz')
B_parser.set_defaults(func=run_command)
args = parser.parse_args()
if args.command is not None:
args.func(parser, args)
else:
parser.print_help()
This generates a help page like so:
~ python args.py -h
usage: PROG [-h] {command1,command2} ...
positional arguments:
{command1,command2} Sub-commands
command1 Command 1
command2 Command 2
optional arguments:
-h, --help show this help message and exit
See '<command> --help' to read about a specific sub-command.
and help text for each sub-command:
~ python args.py B -h
arg.py command2 -h
usage: PROG command2 [-h] [--bar BAR] [--baz BAZ]
optional arguments:
-h, --help show this help message and exit
--bar BAR
--baz BAZ

Python 2.7. Parse input parameters

Good time of the day! First of all, let me say that I'm a newbie in Python world. I've problems with parsing input parameters. At this moment I'm using Python 2.7 and module which is called argparse. I'm trying to design simple application which will be able to parse simple input parameters. Here is a short example:
my_app.py sync --force
Second example:
my_app.py patch --branch
I see that for this I can use add_argument which can work with positional and optional arguments. Like in my case I want to have few positional (but optional at the same time) and few optional arguments.
To do that I've design small script
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='My App')
parser.add_argument('sync', type=bool, const=True, nargs='?')
parser.add_argument('-f', '--force', dest='sync_force', type=bool, const=True, nargs='?')
parser.add_argument('-b', '--branch', type=str, const=True, nargs='?')
parser.add_argument('-u', '--url', type=str, const=True, nargs='?')
parser.add_argument('patch', type=bool, const=True, nargs='?')
parser.add_argument('revert', type=bool, const=True, nargs='?')
parser.add_argument('verify', type=bool, const=True, nargs='?')
values = parser.parse_args()
if values.revert:
handler.revert()
else:
parser.print_help()
I see that I can use nargs='?' to specify the positional parameter as optional, but each time when I'm calling my script it shows like I got 'sync' as input parameter, even if I specified 'patch'. So, I think that it shows just first added element.
Could you tell me what's wrong and where is a problem?
Update:
I'm trying to achieve a situation when I will be able to have only one positional argument at the same time(and at least one, but with any additional optional parameters). For example
my_app.py sync
my_app.py path
my_app.py verify --force
my_app.pyrevert --branch
With:
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='My App')
parser.add_argument('cmd', choices=['sync','patch', 'revert','verify'])
parser.add_argument('-f', '--force', action='store_true')
parser.add_argument('-b', '--branch')
parser.add_argument('-u', '--url')
args = parser.parse_args()
print(args)
if args.cmd in ['revert']:
print('handler.revert()')
else:
parser.print_help()
testing
1243:~/mypy$ python stack60327766.py
usage: stack60327766.py [-h] [-f] [-b BRANCH] [-u URL]
{sync,patch,revert,verify}
stack60327766.py: error: too few arguments
1244:~/mypy$ python stack60327766.py revert
Namespace(branch=None, cmd='revert', force=False, url=None)
handler.revert()
1244:~/mypy$ python stack60327766.py revert -f -b foobar -u aurl
Namespace(branch='foobar', cmd='revert', force=True, url='aurl')
handler.revert()
1244:~/mypy$ python stack60327766.py verify-f -b foobar -u aurl
usage: stack60327766.py [-h] [-f] [-b BRANCH] [-u URL]
{sync,patch,revert,verify}
stack60327766.py: error: argument cmd: invalid choice: 'verify-f' (choose from 'sync', 'patch', 'revert', 'verify')
1245:~/mypy$ python stack60327766.py verify -f -b foobar -u aurl
Namespace(branch='foobar', cmd='verify', force=True, url='aurl')
usage: stack60327766.py [-h] [-f] [-b BRANCH] [-u URL]
{sync,patch,revert,verify}
My App
positional arguments:
{sync,patch,revert,verify}
optional arguments:
-h, --help show this help message and exit
-f, --force
-b BRANCH, --branch BRANCH
-u URL, --url URL
argparse has native support for subcommands.
Adapting from the above-linked documentation,
parser = argparse.ArgumentParser()
parser.add_argument('--foo')
subparsers = parser.add_subparsers()
sync_parser = subparsers.add_parser('sync')
patch_parser = subparsers.add_parser('patch')
revert_parser = subparsers.add_parser('revert')
verify_parser = subparsers.add_parser('verify')
etc. is probably what you're looking for.
Beyond that, I can only recommend the Click library for a more fluent interface for CLIs.

Removing dest variable from help text

While using argparse, the help dialog of my program displays dest variable. How do I remove this?
I tried the add_help=False object but that just removed the help dialog for that option entirely.
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--interface", dest="interface", help="foo")
parser.add_argument("-m", "--mac", dest="mac", help="bar")
I get the following result with INTERFACE and MAC next to my optional arguments:
usage: test.py [-h] [-i INTERFACE] [-m MAC]
optional arguments:
-h, --help show this help message and exit
-i INTERFACE, --interface INTERFACE
foo
-m MAC, --mac MAC bar
How can I remove DEST values from my output?
As #hpaulj suggested, you can set the metavar parameter of each argument to an empty string
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--interface", dest="interface", help="foo", metavar='')
parser.add_argument("-m", "--mac", dest="mac", help="bar", metavar='')
parser.parse_args()
which will output the following result:
usage: test.py [-h] [-i] [-m]
optional arguments:
-h, --help show this help message and exit
-i , --interface foo
-m , --mac bar
Let me know if that's the output you expected.

Python argparse: name parameters

I'm writing a program that use argparse, for parsing some arguments that I need.
for now I have this:
parser.add_argument('--rename', type=str, nargs=2, help='some help')
when I run this script I see this:
optional arguments:
-h, --help show this help message and exit
--rename RENAME RENAME
some help
How can I change my code in that way that the help "page" will show me:
--rename OLDFILE NEWFILE
Can I then use OLDFILE and NEWFILE value in this way?
args.rename.oldfile
args.rename.newfile
If you set metavar=('OLDFILE', 'NEWFILE'):
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--rename', type=str, nargs=2, help='some help',
metavar=('OLDFILE', 'NEWFILE'))
args = parser.parse_args()
print(args)
Then test.py -h yields
usage: test.py [-h] [--rename OLDFILE NEWFILE]
optional arguments:
-h, --help show this help message and exit
--rename OLDFILE NEWFILE
some help
You can then access the arguments with
oldfile, newfile = args.rename
If you really want to access the oldfile with args.rename.oldfile
you could set up a custom action:
import argparse
class RenameAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest,
argparse.Namespace(
**dict(zip(('oldfile', 'newfile'),
values))))
parser = argparse.ArgumentParser()
parser.add_argument('--rename', type=str, nargs=2, help='some help',
metavar=('OLDFILE', 'NEWFILE'),
action=RenameAction)
args = parser.parse_args()
print(args.rename.oldfile)
but it extra code does not really seem worth it to me.
Read the argparse documentation (http://docs.python.org/2.7/library/argparse.html#metavar):
Different values of nargs may cause the metavar to be used multiple times. Providing a tuple to metavar specifies a different display for each of the arguments:
>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('-x', nargs=2)
>>> parser.add_argument('--foo', nargs=2, metavar=('bar', 'baz'))
>>> parser.print_help()
usage: PROG [-h] [-x X X] [--foo bar baz]
optional arguments:
-h, --help show this help message and exit
-x X X
--foo bar baz

Categories

Resources