Questions tagged [argparse]

A Python module for implementing command-line interfaces

argparse is a module for implementing command-line interfaces.

From the module documentation:

The argparse module makes it easy to write user-friendly command-line interfaces. The program defines what arguments it requires, and argparse will figure out how to parse those out of sys.argv. The argparse module also automatically generates help and usage messages and issues errors when users give the program invalid arguments.

argparse was added to the stdlib in 2.7/3.2, deprecating .

Resources:

3535 questions
1
vote
1 answer

Python dictionary access data

I'm having a headache by trying to access data in a dictionary. I'm not sure what I call dictionary is really a correct one but at least the following structure doesn't give me error. From a file configuration.py ... appList = { "app1": { …
MatMat
  • 19
  • 2
1
vote
2 answers

Python: arg-parsing inside of an input function?

So I've been curious to figure out how to do argument-parsing inside of input functions. For example: a = input('enter something here: ') now, lets say, if I input '--url example.com --data some_data_here' for example in a, how would I read the…
Jack
  • 39
  • 5
1
vote
1 answer

Passing arguments to add_argument as a dictionary

I have a program in Python 3.8 which takes multiple different commands (e.g. mode), each of which have a different valid constellation of the same options (i.e., one option might be only valid in two commands, another in all of them, etc.) I…
Vercingatorix
  • 1,838
  • 1
  • 13
  • 22
1
vote
1 answer

Argparse positional multiple choices default subset: Invalid Choice

I'm using argparse to obtain a subset of elements using choices with nargs='*'. If no element is introduced, I want to return by default a subset of the elements, but I get an Invalid Choice error. Here is my sample code: from argparse import…
David Davó
  • 417
  • 1
  • 4
  • 18
1
vote
0 answers

argparse - help text for nested add_argument_group() doesn't appear

I hoped to nest argument groups to better document my options, especially given that it is not possible to add groups of mutually exclusive options. While nested argument groups give no errors at run-time, they do not seem display the relevant help…
1
vote
1 answer

Parsing multiple lists with parameters from the command line in python

I am writing a program that should take multiple files as input, where for each file a list of arguments is given. The call could look like: python myprog.py \ --file picture1.svg --scale 3 --color false \ --file picture2.svg --scale 1 --color…
Sergej Zr
  • 11
  • 3
1
vote
1 answer

argparse sub-commands and groups: Setting help-dialog in sub-command on its own group without being hidden in top-level help-dialog

I'm trying to achieve a program that makes both use of sub-commands (e.g.: program sub-command [options]) and groups (which makes for a fancy help dialog). I have achieved this goal with one minor exception: In order to get the help dialog in its…
1
vote
3 answers

Best architecture for a Python command-line tool with multiple subcommands

I am developing a command-line toolset for a project. The final tool shall support many subcommands, like so foo command1 [--option1 [value]?]* So there can be subcommands like foo create --option1 value -- foo make file1 --option2 --option3 The…
1
vote
1 answer

Is there a way to read data from csv using numpy.genfromtxt from a given directory?

I'm supposed to read in a csv filepath through argparse. So path of a csv file is args.X for example, how do I go about loading this data into a 2d numpy array? For now I was using Data = np.genfromtxt(args.X, delimiter=","), this works when args.X…
Amit Kumar
  • 25
  • 3
1
vote
0 answers

Argparse conditional optional positional arguments?

When using argparse I have to add an extra step to make sure there is a input (either from stdin or file). import sys, argparse parser = argparse.ArgumentParser() parser.add_argument('infile', nargs='?', type=argparse.FileType('r'),…
Ahmad Ismail
  • 11,636
  • 6
  • 52
  • 87
1
vote
1 answer

Is there a way to pass in a str argument containing "$" character?

My python script requires an argument of type str. I am using the ArgumentParser class from argparse. I want to pass in a string containing a "$" char in the middle, e.g. "file$102_a.txt", but when passed to the script the argument is stored as…
amc007
  • 129
  • 7
1
vote
0 answers

Passing multiple sets of arguments

I'm working on a project where I need a company name and a related number as inputs for my script. An example of this would be as follow: python myscript.py "Company 1" 5 The above works fine for a single company but I'd like to add multiple at the…
Krowi
  • 1,565
  • 3
  • 20
  • 40
1
vote
1 answer

Argparse, nargs with 1 required, 1 optional?

I'm looking to provide flexible input into my parser. My use case is I have a python script to run some queries that are date range based. How I want the arg parser to operate is if only one date is supplied, use that date as both the start and…
Jammy
  • 413
  • 1
  • 6
  • 12
1
vote
1 answer

Python argparse - Detect if list argument is passed without anything afterwards

Let's say the user types: ./args.py --create --mem 5 and my parser looks like this: parser.add_argument('--create', nargs='*', default=None) Normally --create takes a list with nargs='*', however in this case the user passed nothing. What I would…
SurpriseDog
  • 462
  • 8
  • 18
1
vote
1 answer

how to argparse a type that is diffrent

I am trying try to make it so the python main.py --point 0,0 be the argument and to throw an argument if it does not follow the format like int, ',' , int I really have tried searching online and found you could do parser.add_argument('--point', …
usersman
  • 23
  • 5