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
3 answers

Python argv "False, false' are recognized as True

I ran my program using the below line python ***.py --if_normalize False but my program recognizes if_normalize variable to be True def parse_args(): parser = argparse.ArgumentParser() parser.add_argument("--work-path", required=True,…
ylee
  • 111
  • 1
  • 1
  • 8
1
vote
2 answers

python project structure with CLI and config script

I have an application and currently it's set so one of the scripts config.py reads data from a config.yaml configuration file and also sets some defaults. This works nicely thus far as everything is defined in one place. I now want to pass the…
aksg87
  • 63
  • 1
  • 9
1
vote
1 answer

Unrecognized arguments error when debugging Python script in VS code

I'm trying to debug the following script in VS code: import os import argparse def parseArgs(): parser = argparse.ArgumentParser() parser.add_argument("-cn", "--cert_names", nargs="+", default=None, help='Provide one or several cert names…
1
vote
1 answer

Using argparse to get a list of unique values from choices

I have a program, for which a argument can take values from a defined list. Every value can only be passed once. At least one value must be chosen. If no values are chosen, the default is that all values were chosen. The following code exhibits the…
LudvigH
  • 3,662
  • 5
  • 31
  • 49
1
vote
1 answer

Print out help message associated with argparser made in another file

I have a specific set up where I wanted to call foo.py both as its own script, and possibly as part of another process in another python file. In order to make its own script, i created a separate function to create an argparser and pass it into the…
Drivebyluna
  • 344
  • 2
  • 14
1
vote
1 answer

GCP dataflow, argparse.ArgumentError using DataflowRunner but not DirectRunner

Dataflow pipeline with runtime arguments runs well using DirectRunner, but encounters argument error when switching to DataflowRunner. File "/home/user/miniconda3/lib/python3.8/site-packages/apache_beam/options/pipeline_options.py", line 124, in…
1
vote
1 answer

Python argparse storing arguments as lists rather than ints. Confusing or correct?

I am using two test scripts to teach myself how to use argparse and subprocess libraries in Python. I am confused about the type=int value in add_argument(). calculator.py: import sys x = int(sys.argv[1]) y =…
Dave
  • 312
  • 3
  • 11
1
vote
2 answers

Parsing args with different set of default values

I have a set of parameters for training and a set of parameters for tuning. They share the same name but different default values. I'd like to use argparse to define which group of default values to use and also parse the values. I have learned it…
1
vote
1 answer

How to make bash complete only working on exact position, then back to normal

For example, I have a python script "control_center.py" which takes the $1 (start stop reboot) as command, and extra options (-a foo/bar). control_center.py [start|stop|reboot] -a [foo|bar] I have used argcomplete, it works fine with the…
Mingo Pan
  • 37
  • 5
1
vote
0 answers

Divide program into working callable modules

I have a program, that performs (in logical terms) 3 different operations. So far i've written a module and a main.py script that imports module's files and executes. Structure: ── jasper │   ├── crispr.py │   ├── database.py │   ├── __init__.py │  …
777moneymaker
  • 697
  • 4
  • 15
1
vote
2 answers

How to pass a date range to a script?

I have the following piece of code: def parse_args(): """Takes in arguments from command when run :return: date """ parser = argparse.ArgumentParser(sys.argv) parser.add_argument('-d') args = parser.parse_args() d_param…
Avi G
  • 67
  • 7
1
vote
0 answers

argparse: determine if the value is default or entered by user

I have the following code: import argparse parser = argparse.ArgumentParser() parser.add_argument('-option', choices=['a','b','c'],type=str, default='a') args = parser.parse_args(['-option','a']) print(args) resulting namespace…
1
vote
3 answers

Python "del" syntax error when used as a variable name

I am building a todo-list CLI and it requires a del argument for deleting an entry from the list. The CLI usage is as given below $ ./todo help Usage :- $ ./todo add "todo item" # Add a new todo $ ./todo ls # Show remaining todos $…
Sarath Sajan
  • 45
  • 2
  • 10
1
vote
1 answer

Get arguments from ArgumentParser without calling parse_args

I have the following code and I want to use to extract the config parameter. parser = argparse.ArgumentParser() parser.add_argument( "--config", type=str, default="src/config.yml", dest="config" ) My issue is that I cannot use…
David Masip
  • 2,146
  • 1
  • 26
  • 46
1
vote
1 answer

Optional flag that works by itself or with all other arguments (like -h)

I realize the title might be confusing, but I didn't know how to word my problem. My program's command line syntax looks like this: conv.py val from to, where to is optional, but I don't think that should matter. I'm trying to add a flag that forces…
SvbZ3r0
  • 638
  • 4
  • 19