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

parser.parse_args boolean parse "False" as True

When I run my python code with -rm False I still see _REMOVE_ADS = True. How can I fix this? parser.add_argument( "-rm", "--remove_ads", type=bool, required=False, default=False, help="Should remove disapproved ads. Default:…
Elad Benda
  • 35,076
  • 87
  • 265
  • 471
1
vote
2 answers

How to use "argparse" argument across python files?

Let's say I have a main.py and a subfile.py. main.py import subfile #Writen by myself. I want to share argparse with it import argparse parser =…
WBR
  • 57
  • 1
  • 11
1
vote
1 answer

How to make arguments to mutually exculsive group?

I have a mutually exclusive group where the user can choose from --execute, --delete, --create. How can I make arguments only available for specific groups? For example --execute {filename}, --create -fn {filename} -p {path} and --delete {filename}.…
1
vote
1 answer

Loading dataset using command line in Python

I know that I can load the datasets in the pandas using the read_csv function of pandas as below import pandas as pd df = read_csv('/home/user/iris_dataset.csv', header=0) df.head() How can I load the same dataset using the command line arguments…
user3046211
  • 466
  • 2
  • 13
1
vote
2 answers

How to modify a tsv-file column with Python

I have a GFF3 file (mainly a TSV file with 9 columns) and I'm trying to make some changes in the first column of my file in order to overwrite the modification to the file itself. The GFF3 file looks like this: ## GFF3 file ## replicon1 ##…
1
vote
3 answers

How to specify argparse options from an external file?

I am using argparse to specify some arguments as follows: my_parser = argparse.ArgumentParser() my_parser.add_argument("--script_path", nargs='?', type=str, const='', default='', help="Path of the script to pull.") my_parser.add_argument("--script",…
Lev
  • 673
  • 1
  • 12
  • 29
1
vote
0 answers

python argparse mutally exclusive group

I want to do this using python argparse module myprog [-f val_f | [-a val_a -b val_b]] Both arguments -a and -b can be used at the same time. Or can be used one argument. But argument -f can't be used with any of argument -a or -b. Valid myprog -f…
Hirusha Fernando
  • 1,156
  • 10
  • 29
1
vote
4 answers

Running argparse with optional arguments that won't require specifying output filename

I have read through a bunch of the questions already answered, but I don't see this covered--at least not that I recognized. I am using argparse to take a file and convert it to a different type. The input filename is required. The output filename…
mdign002
  • 89
  • 1
  • 1
  • 7
1
vote
1 answer

Python argparse help option won't display all of the arguments when using parse_known_args()

Essentially, I have a program that takes in a few optional/positional arguments but I want an optional argument that can be used on its own. I want it to work similar to how the "--version" option works in that if that argument is given, no…
1
vote
3 answers

Python argument parser - parse only from console

Can I extract only the parsed arguments from the command line, ignoring all default arguments? If the user passed the default value for an argument, I would like it to appear in the subset as well: parser =…
DsCpp
  • 2,259
  • 3
  • 18
  • 46
1
vote
1 answer

How do I make a pair of optional parameters contingent on each other in Python?

I have two parameters that I need either both or neither of. I thought of two solutions: one optional parameter with nargs=2, or two optional parameters that are contingent on each other. I found answers for nargs=2, but the help output seems messy…
Beweeted
  • 319
  • 2
  • 7
1
vote
0 answers

Python argparse fails when the script run without preceding py -2 or -3

My python code is: # my_script.py import argparse parser = argparse.ArgumentParser() parser.add_argument("-v", "--verbose", help="increase output verbosity", action="store_true") args = parser.parse_args() if args.verbose: …
Halona
  • 1,475
  • 1
  • 15
  • 26
1
vote
2 answers

python argparse separate 2 options of comamnds

I've read the documentation but still can't figure out how to achieve the following behavior: 1.likes. Give a specified number of likes to users in a tinder (in the future, this may contain more options, such as "gender", "frequency", "age",…
salius
  • 918
  • 1
  • 14
  • 30
1
vote
1 answer

Positional argument for subparser raises the error: invalid choice

I am new to argsparse thus this might pretty sure be a newbie mistake so please bare with me. My code: import argsparse parent_parser = argparse.ArgumentParser(description='Argument Parser') subparsers =…
user13581602
  • 105
  • 1
  • 9
1
vote
1 answer

ArgParse: Check if argument value present or not, else use default

I want to store TRUE/FALSE when the parameters are passed in the command line. But if nothing is present after the parameter name or the parameter isn't specified, it should store TRUE. The code below does it for the most part, except that it stores…