argparse

Python Unicode Encoding

无人久伴 提交于 2019-12-23 10:38:06
问题 I am using argparse to read in arguments for my python code. One of those inputs is a title of a file [ title ] which can contain Unicode characters. I have been using 22少女時代22 as a test string. I need to write the value of the input title to a file, but when I try to convert the string to UTF-8 it always throws an error: UnicodeDecodeError: 'ascii' codec can't decode byte 0x8f in position 2: ordinal not in range(128) I have been looking around and see I need my string to be in the form u"foo

Setting command line arguments for main function tests

纵饮孤独 提交于 2019-12-23 08:24:35
问题 I have a main() function in python that gets command line arguments. Is there a way for me to write pytest tests for this function and define the arguments in the code? e.g. def main(): # argparse code args, other = arg_parser.parse_known_args() return args.first_arg def test_main(): res = main() # call with first_arg = "abc" assert(res == "abc") 回答1: parse_args takes a argv parameter. The docs uses this repeatedly in it's examples parser = argparse.ArgumentParser() parser.add_argument('--foo

Setting command line arguments for main function tests

两盒软妹~` 提交于 2019-12-23 08:24:11
问题 I have a main() function in python that gets command line arguments. Is there a way for me to write pytest tests for this function and define the arguments in the code? e.g. def main(): # argparse code args, other = arg_parser.parse_known_args() return args.first_arg def test_main(): res = main() # call with first_arg = "abc" assert(res == "abc") 回答1: parse_args takes a argv parameter. The docs uses this repeatedly in it's examples parser = argparse.ArgumentParser() parser.add_argument('--foo

python argparse - pass values WITHOUT command line

巧了我就是萌 提交于 2019-12-23 08:03:09
问题 I think I'm not understanding something basic about python's argparse. I am trying to use the Google YouTube API for python script, but I am not understanding how to pass values to the script without using the command line. For example, here is the example for the API. The examples on github and elsewhere show this example as being called from the command line, from where the argparse values are passed when the script is called. I don't want to use the command line. I am building an app that

python argparse - pass values WITHOUT command line

醉酒当歌 提交于 2019-12-23 08:01:03
问题 I think I'm not understanding something basic about python's argparse. I am trying to use the Google YouTube API for python script, but I am not understanding how to pass values to the script without using the command line. For example, here is the example for the API. The examples on github and elsewhere show this example as being called from the command line, from where the argparse values are passed when the script is called. I don't want to use the command line. I am building an app that

Python argparse toggle flags

偶尔善良 提交于 2019-12-23 07:59:32
问题 Is there any way in argparse to parse flags like [+-]a,b,c,d ? foo.py +s -b should store True in the dest of s and False in the dest of b , much like done by the Windows attrib or the Linux chmod . Currently, I am using 2 separate arguments +s and -s with store_true and store_false , respectively. But it creates an ugly help with it listing each flag twice (+a & -a) Another workaround would be to manually parse the extended arg with regex (which somehow seems a lot easier and use custom

How can I create an argparse mutually exclusive group with multiple positional parameters?

半世苍凉 提交于 2019-12-23 07:39:42
问题 I'm trying to parse command-line arguments such that the three possibilities below are possible: script script file1 file2 file3 … script -p pattern Thus, the list of files is optional. If a -p pattern option is specified, then nothing else can be on the command line. Said in a "usage" format, it would probably look like this: script [-p pattern | file [file …]] I thought the way to do this with Python's argparse module would be like this: parser = argparse.ArgumentParser(prog=base) group =

Python argparse: list individual choices in the usage

二次信任 提交于 2019-12-23 04:50:12
问题 I have a program which takes multiple arguments, e.g. breakfast.py --customer=vikings eggs sausage bacon where "eggs", "sausage" and "bacon" can be specified from a list of specific choices. Now I like the output of breakfast.py --help to look like this: usage: breakfast.py [-h] [--customer CUSTOMER] INGREDIENT [INGREDIENT ...] positional arguments: your choice of ingredients: bacon Lovely bacon egg The runny kind sausage Just a roll spam Glorious SPAM tomato Sliced and diced optional

using a variable keyword for an optional argument name with python argparse

拟墨画扇 提交于 2019-12-23 04:43:11
问题 I am using argparse for a python script I am writing. The purpose of the script is to process a large ascii file storing tabular data. The script just provides a convenient front-end for a class I have written that allows an arbitrary number of on-the-fly cuts to be made on the tabular data. In the class, the user can pass in a variable-name keyword argument with a two-element tuple bound to the variable. The tuple defines a lower and upper bound on whatever column with a name that

How to add positional options for existing arguments in argparse

别来无恙 提交于 2019-12-22 18:59:42
问题 I'm dealing with a (Python 3.x) script (written by someone else) where the input and output are currently specified with flagged optional arguments like so: parser.add_argument('-i', '--input', nargs='?', type = argparse.FileType('r'), default=sys.stdin, dest='inputfile') parser.add_argument('-o', '--output-file', nargs='?', type=argparse.FileType('w'), default=sys.stdout, dest='outputfile') I'd like to upgrade this script so that the input and output file can be specified as positional