argparse

Using argparse.REMAINDER at beginning of parser / sub parser

好久不见. 提交于 2019-12-24 01:54:10
问题 I want to implement an arg parser that allows me to run unittests as one of the sub commands, blindly passing the arguments on to unittest.main(). e.g., $ foo.py unittest [args to pass to unittest.main()] along with other sub commands: $ foo.py foo ... $ foo.py bar ... Following argparse's example, this works: #!/usr/bin/python import argparse p = argparse.ArgumentParser(prog='PROG') p.add_argument('-v', '--verbose', action='store_true') sub = p.add_subparsers(dest='cmd') foo = sub.add_parser

Python argparse - mandatory argument - either positional or optional

不打扰是莪最后的温柔 提交于 2019-12-24 00:45:25
问题 I want the user to be able to pass a mandatory argument to 'argparse', with either positional, or optional argument. I.e., both following forms are valid: my_prog arg my_prog -m arg I've seen Argparse optional positional arguments? But the suggestions there make both form optional. I want that one of them will be mandatory. Of course, I can add a manual checking after parsing that at least one of them has been set. But I got a hunch that there must be a better solution. (And even with my

How can I localize argparse generated messages in a portable way?

ε祈祈猫儿з 提交于 2019-12-24 00:24:34
问题 Context: I am developping a small module to automatically rename photographs in a directory according to their exif timestamp (goal: easily mixing pictures from different cameras or smartphones). It works smoothly either as a Python package or directly from the command line through a tiny wrapper using argparse. And I have just had the (rather stupid) idea to localize it in non English language. Ok, gettext is my friend for all my own code, but when I came to agparse generated messages, I

Python argparse --toggle --no-toggle flag

旧时模样 提交于 2019-12-23 17:08:38
问题 Is there a straightforward way to use --toggle and --no-toggle flags with Python's argparse? Right now I'm using something similar to the following: import argparse parser = argparse.ArgumentParser() parser.add_argument('--toggle', action='store_true', dest='toggle') parser.add_argument('--no-toggle', action='store_true', default=True, dest='notoggle') options = parser.parse_args([]) I'm just manually parsing out the possibilities in a long if chain, but it would be nice if there was a way to

Framework argparse - check if flag is set

不羁的心 提交于 2019-12-23 16:58:32
问题 I want to use my script in this way: python script.py -x now I run it using this command python script.py -x y My code: parser = ArgumentParser() parser.add_argument('-x', '--x', dest="x", default="n") options = parser.parse_args() if option.x == 'y': f() It is possible to write it in this way python script.py -x parser = ArgumentParser() parser.add_argument('-x', '--x', dest="x") options = parser.parse_args() if isset(option.x): f() 回答1: Just use the 'store_true' action: import argparse

Python `argparse`: Is there a clean way to add a flag that sets multiple flags (e.g. `--all`" is equivalent to `--x --y`)

拟墨画扇 提交于 2019-12-23 16:42:51
问题 I've got some Python argparse command-line processing code that initially looked like this: import argparse ap = argparse.ArgumentParser() ap.add_argument("--x", help = "Set `x`.", action = "store_true", default = False) ap.add_argument("--y", help = "Set `y`.", action = "store_true", default = False) ap.add_argument( "--all", help = "Equivalent to `--x --y`.", action = "store_true", default = False ) args = ap.parse_args() if args.all: args.x = True args.y = True print "args.x", args.x print

how to add multiple argument options in python using argparse?

Deadly 提交于 2019-12-23 13:58:08
问题 My Requirement: For now when I run my python application with this command python main.py -d listhere/users.txt The program will run and save the result file as predefined name say reports.txt Now I want to add this functionality to allow users to choose what to put the filename and where to save as so python main.py -d -o output/newfilname -i listhere/users.txt Everything is same but I want another argument -o to be passed which will determine the filpath and name to be saved. How do I do it

how to add multiple argument options in python using argparse?

我的梦境 提交于 2019-12-23 13:57:27
问题 My Requirement: For now when I run my python application with this command python main.py -d listhere/users.txt The program will run and save the result file as predefined name say reports.txt Now I want to add this functionality to allow users to choose what to put the filename and where to save as so python main.py -d -o output/newfilname -i listhere/users.txt Everything is same but I want another argument -o to be passed which will determine the filpath and name to be saved. How do I do it

Why isn't fromfile-prefix-chars in Python argparse working?

帅比萌擦擦* 提交于 2019-12-23 13:07:31
问题 I'm trying to use the fromfile-prefix-chars feature of argparse in Python to load all my command line arguments from a file, but it keeps complaining that I haven't specified some argument. The code: import argparse def go(): parser = argparse.ArgumentParser(fromfile_prefix_chars='@') parser.add_argument("--option1") parser.add_argument("--option2", type=int, required=True) args = parser.parse_args() if __name__ == "__main__": go() The argument file: --option1 foo --option2 1234 The command

argparse missing in python 3

╄→гoц情女王★ 提交于 2019-12-23 10:51:02
问题 does somebody know, why the argparse module didn't make it in python 3? it's new in python 2.7, but the 2.x branch is running out with 2.7. it makes no sense to me not to support it in the actual python 3 branch. 回答1: It will be in Python 3.2. It was just added in Python 2.7, which was released just this July; Python 3.2 will be the next 3.x release after that date. 回答2: argparse is in Python 3, 3.2 to be specific. See also: http://www.python.org/dev/peps/pep-0389/ 来源: https://stackoverflow