argparse

Sphinx and argparse - autodocumenting command line scripts?

青春壹個敷衍的年華 提交于 2019-12-17 18:48:19
问题 I'm building a Python package, and using Sphinx to create the docs. Aside from my package code, I also include a lot of command line Python scripts, which use argparse. I was wondering if there is a way to get Sphinx to autodocument these scripts? The end goal would be a pretty-printed list of scripts, with the associated help print, arguments and options. And to be clear, I'm looking for a pre-existing way to do this, not a way to implement this myself. This isn't as specific of a question

How can I use python's argparse with a predefined argument string?

試著忘記壹切 提交于 2019-12-17 18:37:36
问题 I want to use the pythons argparse module to parse my cli parameter string. This works for the parameters a pass from terminal, but not with a given string. import argparse parser = argparse.ArgumentParser(description='Argparse Test script') parser.add_argument("param", help='some parameter') argString = 'someTestFile' print(argString) args = parser.parse_args(argString) If I run this script I get this output: ~/someTestFile usage: argparsetest.py [-h] param argparsetest.py: error:

Setting options from environment variables when using argparse

大兔子大兔子 提交于 2019-12-17 17:49:08
问题 I have a script which has certain options that can either be passed on the command line, or from environment variables. The CLI should take precedence if both are present, and an error occur if neither are set. I could check that the option is assigned after parsing, but I prefer to let argparse to do the heavy lifting and be responsible for displaying the usage statement if parsing fails. I have come up with a couple of alternative approaches to this (which I will post below as answers so

Argparse: Way to include default values in '--help'?

你。 提交于 2019-12-17 17:21:04
问题 Suppose I have the following argparse snippet: diags.cmdln_parser.add_argument( '--scan-time', action = 'store', nargs = '?', type = int, default = 5, help = "Wait SCAN-TIME seconds between status checks.") Currently, --help returns: usage: connection_check.py [-h] [--version] [--scan-time [SCAN_TIME]] Test the reliability/uptime of a connection. optional arguments: -h, --help show this help message and exit --version show program's version number and exit --scan-time [SCAN_TIME] Wait SCAN

Python argparse: Lots of choices results in ugly help output

妖精的绣舞 提交于 2019-12-17 15:37:44
问题 I have this code which I am generally pleased with: import argparse servers = [ "ApaServer", "BananServer", "GulServer", "SolServer", "RymdServer", "SkeppServer", "HavsServer", "PiratServer", "SvartServer", "NattServer", "SovServer" ] parser = argparse.ArgumentParser(description="A program to update components on servers.") group = parser.add_mutually_exclusive_group() group.add_argument('-l', '--list', dest="update", action='store_false', default=False, help='list server components') group

type=dict in argparse.add_argument()

↘锁芯ラ 提交于 2019-12-17 11:28:20
问题 I'm trying to set up a dictionary as optional argument (using argparse); the following line is what I have so far: parser.add_argument('-i','--image', type=dict, help='Generate an image map from the input file (syntax: {\'name\': <name>, \'voids\': \'#08080808\', \'0\': \'#00ff00ff\', \'100%%\': \'#ff00ff00\'}).') But running the script: $ ./script.py -i {'name': 'img.png','voids': '#00ff00ff','0': '#ff00ff00','100%': '#f80654ff'} script.py: error: argument -i/--image: invalid dict value: '

type=dict in argparse.add_argument()

谁都会走 提交于 2019-12-17 11:28:14
问题 I'm trying to set up a dictionary as optional argument (using argparse); the following line is what I have so far: parser.add_argument('-i','--image', type=dict, help='Generate an image map from the input file (syntax: {\'name\': <name>, \'voids\': \'#08080808\', \'0\': \'#00ff00ff\', \'100%%\': \'#ff00ff00\'}).') But running the script: $ ./script.py -i {'name': 'img.png','voids': '#00ff00ff','0': '#ff00ff00','100%': '#f80654ff'} script.py: error: argument -i/--image: invalid dict value: '

How to open file using argparse?

混江龙づ霸主 提交于 2019-12-17 07:13:06
问题 I want to open file for reading using argparse. In cmd it must look like: my_program.py /filepath That's my try: parser = argparse.ArgumentParser() parser.add_argument('file', type = file) args = parser.parse_args() 回答1: The type of the argument should be string (which is default anyway). So make it like this: parser = argparse.ArgumentParser() parser.add_argument('filename') args = parser.parse_args() with open(args.filename) as file: # do stuff here 回答2: Take a look at the documentation:

Python argparse ignore unrecognised arguments

早过忘川 提交于 2019-12-17 07:12:53
问题 Optparse, the old version just ignores all unrecognised arguments and carries on. In most situations, this isn't ideal and was changed in argparse. But there are a few situations where you want to ignore any unrecognised arguments and parse the ones you've specified. For example: parser = argparse.ArgumentParser() parser.add_argument('--foo', dest="foo") parser.parse_args() $python myscript.py --foo 1 --bar 2 error: unrecognized arguments: --bar Is there anyway to overwrite this? 回答1: Replace

How to open file using argparse?

心不动则不痛 提交于 2019-12-17 07:12:39
问题 I want to open file for reading using argparse. In cmd it must look like: my_program.py /filepath That's my try: parser = argparse.ArgumentParser() parser.add_argument('file', type = file) args = parser.parse_args() 回答1: The type of the argument should be string (which is default anyway). So make it like this: parser = argparse.ArgumentParser() parser.add_argument('filename') args = parser.parse_args() with open(args.filename) as file: # do stuff here 回答2: Take a look at the documentation: