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

Passing configs from `.txt` file into argparse? (Similar to youtube-dl's `--config-location`)

I have a program with ~20 configs, which I would like to be accessed from the command-line via argparse, either by something like --config_1 'This is the first config' or --config-location , where the path to the config file…
Wilan
  • 147
  • 1
  • 12
1
vote
3 answers

How can I setup a python CLI application so that I can use it without directly referring to the interpreter?

I want to build an application, say it's called helloworld, with a command line interface in python. My application as multiple nested modules and a top level module main.py. Say the layout is : helloworld/ helloworld/ __init__.py …
Alex
  • 33
  • 7
1
vote
0 answers

AttributeError: 'int' object has no attribute 'value' python Error

I'm learning basics of Python and got already stuck at the beginning of argparse tutorial. I'm getting the following error: AttributeError: 'int' object has no attribute 'value': batch_size = point_cloud.get_shape()[0].value in:def…
adin
  • 21
  • 5
1
vote
1 answer

How do I print nextflow script parameters in arparse style?

I would like to print the parameters of my nextflow script as a help menu (e.g., nextflow run main.nf --help), similar to the way arparse does it in python (i.e., script --help). I have trawled the web and help docs and have found how to print a…
user3479780
  • 525
  • 7
  • 18
1
vote
2 answers

Difference between Python on VSCode vs PyChamp or Console Python

A little bit of context...I'm new to python so, Since I am new to the language I've started by googling the best IDE to use with python, many sites recommended Pycharm so I started using it, But since I use VSCode at work I decided to try it but…
Gatunox
  • 532
  • 4
  • 14
1
vote
2 answers

Python ArgParse works even though script is called with argument with similar name

I have the following code: import argparse # Create the parser parser = argparse.ArgumentParser("ArgParse intro") # Add an argument mand_arg_group = parser.add_argument_group("Mandatory arguments") mand_arg_group.add_argument('--name', type = str,…
padurean04
  • 27
  • 5
1
vote
1 answer

How to set custom error messages for argparse python module

I want to change default message for errors caused by typing wrong argument value or typing argument without any value. I have code test.py: import argparse parser = argparse.ArgumentParser() parser.add_argument('-n', …
Deadjim
  • 27
  • 6
1
vote
1 answer

Python convert List[str] to Dict[str, str] using ArgumentParser

I need to parse command line argument - convert List[str] to Dict[str, str] ['key1=value1', 'key2=value2'] -> {key1: value1, key2:value2} I use the following code: parser.add_argument( '--model-config', nargs='+', …
Vlad Aleshin
  • 361
  • 4
  • 15
1
vote
1 answer

Argparse outputting help text twice

After an hour googling, I can't find anybody who has had anything resembling this issue besides myself. I created a command line interface with argparse. Originally I had tried to leverage argparse's built in help text behavior. But my boss isn't…
1
vote
1 answer

Traceback with argparse and ArgumentError

I'm using Python version 3.10.1. I'm trying to use the argparse library to handle arguments, including handling invalid arguments gracefully. However, I'm encountering a mind-boggling traceback. Here's the problem code: import argparse def…
Alureon
  • 179
  • 1
  • 3
  • 14
1
vote
2 answers

Some python argparse usage questions: retrieving arguments

Some questions that I couldn't figure out from reading the docs and some other questions. 1: I was stumped by "how do I actually retrieve the arguments" so I looked around and someone suggested to use the __dict __ function to access it like a…
MxLDevs
  • 19,048
  • 36
  • 123
  • 194
1
vote
2 answers

argparse argument order issue

I am using subparsers to implement the following function: import argparse parser = argparse.ArgumentParser(prog='dj') parser.add_argument('--log','-l',type=str, help='log file') parser.add_argument('--parser','-p',type=bool, nargs='?',…
1
vote
1 answer

Pyinstaller not allowing multiprocessing with MacOS

I have a python file that I would like to package as an executable for MacOS 11.6. The python file (called Service.py) relies on one other json file and runs perfectly fine when running with python. My file uses argparse as the arguments can differ…
PKen
  • 91
  • 2
  • 14
1
vote
0 answers

Python: The proper way to declare class instance parameters when initialising argparse.ArgumentParser object inside a class

I'm trying to write a Python script to create a CLI command. To make it more readable, I'm creating argparse.ArgumentParser object directly in the class __init__() (not in the '__main__' part of the script as is usually the case in tutorials). The…
Myklebost
  • 59
  • 8
1
vote
1 answer

How do I append plain arguments and flag arguments into a single array using argparse?

I want to do something like the following: parser = argparse.ArgumentParser(description='Strings and ints in one array') parser.add_argument('values', metavar='STRING', nargs='*', action='append',…
Matt
  • 21,026
  • 18
  • 63
  • 115