argparse

Python argparse: Require two corequisite positional arguments

会有一股神秘感。 提交于 2019-12-25 04:09:44
问题 Using argparse, how do I specify that I want two positional arguments to appear together or not at all? I.e. I want my usage string to look like: Usage: FooBar.py [-h] [FOO BAR] 回答1: As suggested by @hpaulj, here's a solution you could use: In [1]: import argparse In [2]: parser = argparse.ArgumentParser(description="bla bla") In [3]: parser.add_argument("--foo", nargs=2, help="a foo argument") In [4]: parser.parse_args(["--foo", "1", "2"]) Out[4]: Namespace(foo=['1', '2']) In [5]: parser

How to create subparser with argparse from existing program in Python 3?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 03:55:41
问题 Original post: If one has an executable mini_program.py that uses argparse with the following structure: def main(): parser = argparse.ArgumentParser() parser.add_argument('-X', '--attribute_matrix', type=str, help = 'Input: Path/to/Tab-separated-value.tsv') parser.add_argument('-y', '--target_vector', type=str, help = 'Input: Path/to/Tab-separated-value.tsv') opts = parser.parse_args() if __name__ == "__main__": main() How can one create a controller program parent_program.py that uses

Python3 Argparse metavar brackets parsed weirdly

 ̄綄美尐妖づ 提交于 2019-12-25 03:33:23
问题 I am using argparse in python3, and I get some strange things: A short version of code that I'm using is: argparser = argparse.ArgumentParser(description='add/remove items') argparser.add_argument('-a', action='append', metavar="Item(s)", help='add one or more items to the list') argparser.add_argument('-r', action='append', metavar="Item(s)", help='remove one or more items from the list') args = argparser.parse_args() When I run the script with the -h flag, I get this output: usage: test.py

Python: Can optparse have the ACTION attribute to act both like STORE and STORE_TRUE?

最后都变了- 提交于 2019-12-25 02:08:20
问题 I am using optparse to get command line input. Lets say that I am running a script demo.py and it creates some output. But unless I specify the command line input, the output is not written to a file. I am trying to do the following: python demo.py in command line should run the script, but not write the output anywhere. python demo.py -o in command line should write the output to my default file name output.txt . python demo.py -o demooutput.txt in command line should write the output to

Python argparse, Value after positional argument

自闭症网瘾萝莉.ら 提交于 2019-12-25 01:45:51
问题 So I'm writing this very small program to do http get and post requests. The requests are as follows: requestApp.py help requestApp.py help get requestApp.py help post requestApp.py get [-v] [-h key:value] URL requestApp.py post [-v] [-h key:value] [-d inline-data] [-f file] URL As you can see, the -v, -h, -d, -f, URL arguments are optional. The get and post arguments are non-optional. I'll show you the snippet of my program that is relevant to this situation: parser = argparse.ArgumentParser

Python argparse mutually exclusive with stdin being one of the options

◇◆丶佛笑我妖孽 提交于 2019-12-24 23:46:15
问题 I would like my script to receive these mutually exclusive input options: an input file containing a JSON ( script.py -i input.json ); a string containing a JSON ( script.py '{"a":1}' ); a JSON from stdin ( echo '{"a":1}' | script.py or cat input.json | script.py ). and these mutually exclusive output options : an output file containing a JSON; a JSON in stdout. So I tried with this code import json,sys,argparse parser = argparse.ArgumentParser(description='Template for python script managing

python, argparse get argument from input- raw_input

≡放荡痞女 提交于 2019-12-24 22:09:57
问题 I need help with this issue, I want to take an argument from user input using argparse save it to a variable and print out the result Example: Message to send : -t Greeting -m hello guys how are you prints out : greeting hello guys how are you this is my code: import argparse, shlex parser = argparse.ArgumentParser() parser.add_argument('-t', action='store', dest='title') parser.add_argument('-m', action='store', dest='msg') command = raw_input('message to send') args = parser.parse_args

Argparse pattern where input filenames are given through a file or a list of filenames on the command line

两盒软妹~` 提交于 2019-12-24 21:42:37
问题 Does argparse support a pattern such as: foo.py {-f list_of_filenames.txt|file [file ...]} I achieve this at the moment with the following argparse definition: parser = argparse.ArgumentParser() parser.add_argument("--file", "-f") parser.add_argument("files", nargs="*") and performing the mutual exclusivity + required check in my code rather than letting argparse do the check. Can argparse handle a pattern of this sort? 回答1: argparse takes fromfile-prefix-chars parameter https://docs.python

Argparse `append` not working as expected [duplicate]

佐手、 提交于 2019-12-24 18:13:23
问题 This question already has answers here : Can't get argparse to read quoted string with dashes in it? (4 answers) Closed 2 years ago . I am trying to configure argparse to allow me to specify arguments that will be passed onto another module down the road. My desired functionality would allow me to insert arguments such as -A "-f filepath" -A "-t" and produce a list such as ['-f filepath', '-t'] . In the docs it seems that adding action='append' should do exactly this - however I am getting an

Multiple level argparse subparsers

喜夏-厌秋 提交于 2019-12-24 16:02:48
问题 I have multiple levels of subparsers within subparsers, but when I run the program with help flag, I see help messages and choices only for top level options. How can I see help for all suboptions, or for specific suboption in deeper level? 回答1: To get the help for a subparser, use a command like python prog.py cmd1 -h . To get the help for a sub-subparser, python prog.py cmd1 cmd12 -h should work. There isn't a means, with the default help mechanism, to show the help for the main parser and