3,637 questions
2
votes
1
answer
77
views
How do I change argparse positional argument destination name?
I am having trouble using add_argument's dest parameter with positional arguments. The reason I want this is that I use nargs='*' and it makes sense to me to have the argument name be singular so that ...
0
votes
1
answer
52
views
Check if Python argparse used explicit CLI argument or default value?
I derive some settings from my parser that I latter store on disk to be reloaded again, during the reload I want to overwrite some values but keep the first ones, similar to new default values.
I want ...
1
vote
2
answers
81
views
Running Uvicorn on Mac command line results in error uvicorn: error: unrecognized arguments [closed]
When I run my FastAPI app with the Uvicorn command, the command line args are not recognized:
(env) mydir$ uvicorn main:app --port 8000 --host 0.0.0.0 --reload
...
uvicorn: error: unrecognized ...
0
votes
0
answers
56
views
Why unrecognized Argparse arg1 in bare "my_script .py arg1" vs "python my_script.py arg1"
I can run a script on the command line with an argument either as the bare "script.py arg1" or I can run it as "python script.py arg1". Both work until I use argparse for command ...
1
vote
3
answers
139
views
Create a common CLI entry point in Python
First of all, I cannot share the original code so I apologize in advance if the example used does not make sense.
I also know the code I present has some issues and will not run as it is, but it is ...
1
vote
1
answer
98
views
How to make argparse accept either subcommands or a direct argument?
I have a CLI tool for managing Docker containers. Here's my current parser setup:
def create_parser():
parser = argparse.ArgumentParser(description="Docker container manager")
...
0
votes
1
answer
77
views
Handle an option only if another option is used
I need to process command-line options from my Python script that correspond to the syntax
tesy.py [-h] [-a [-b]]
(This is just a simplistic example illustrating the problem; in reality my script has ...
3
votes
0
answers
53
views
Optional flag accepted only if particular other flag(s) provided
Scenario:
Flags -a 'thing1' and/or -b 'thing2' can be provided.
If either is provided, 'sub' flags -c and/or -d can be provided. * If -a and/or -b are not provided, -c and/or -d should be ignored/...
0
votes
1
answer
287
views
How can I pass a list as a command-line argument with argparse in Java
Using the Java port of the Argparse4j library, how can I pass a list as a cli argument? I know there are answers to this querstion in Python, but am looking for a Java example.
For example how can I ...
0
votes
0
answers
86
views
How should I type a function that takes the return value of ArgumentParser.add_subparsers as parameter?
I'm type hinting a codebase that uses argparse to parse command-line arguments, and in this part of the code there are some functions that make operations on the value returned by argparse....
0
votes
1
answer
64
views
Check if ArgParse optional argument is set, default value supplied
I'm trying to check if an optional argument is supplied by the user but I want to set its default value if it is not supplied. If I try to use if statement by exploiting None, the default in help ...
0
votes
1
answer
179
views
How to use Python argparse to have lazily loaded subcommands
I have a python package with a lot of sub commands and sub-sub commands. It is organized somewhat like this:
main.py
import argparse
from sum import prepare_arg_parser as prepare_sum_parser
from sub ...
2
votes
1
answer
76
views
argparse parse arbitary number groups of arguments
I have this snippet for illustrative purpose that doesn't work at all.
#!/usr/bin/env python3
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--action", nargs=&...
0
votes
1
answer
152
views
How to use "--" as an argument in python argparser
I have the following python code set up, in which the program would return "something" whenever the user pass -- as an argument:
#script.py
import argparse
parser = argparse.ArgumentParser(...
0
votes
0
answers
121
views
Python argparse with bash alias
I have the following bash script that I source in my root directory:
ROOT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
eval &...
1
vote
2
answers
66
views
How can I add several help arguments using argparse, which don't require adding any required arguments?
I'm using ArgumentParser to document the usage of a complicated program. Since the program has several long lists of capabilities, it requires several --help arguments.
My current ArgumentParser looks ...
3
votes
3
answers
2k
views
How to make argparse work nicely with enums and default values?
I have an enum:
from enum import auto, Enum
class MyEnum(Enum):
ONE = auto()
TWO = auto()
THREE = auto()
and I want to use it as an argument with argparse. To be more specific, I want to ...
1
vote
2
answers
110
views
How can I add a keyword argument with a default value when setting up argparse in Python?
I have a function that sets what arguments can be passed to a command line when calling on a function, using argparse:
def parse_arguments(args):
parser = argparse.ArgumentParser(usage=...
-2
votes
2
answers
157
views
How to incorporate argparse into a python function with two arguments
I am trying to figure out how to incorporate argparse into a python function I have created that accepts two arguments, so that it is executable in the command line.
For example in this function below:...
0
votes
2
answers
52
views
How to get arguments from ArgParser to write to a textfile so it can be read with `fromfile_prefix_chars`
I want a script to save all arguments (whether provided or populate from defaults or read from a text file) to a text file in such a format that I can re-run the script using fromfile_prefix_chars ...
-1
votes
1
answer
82
views
How to get " sign for string args in python argparse
I want to write a program in Python that gets args of unspecified number and type. I can do this with argparse as follows:
parser = argparse.ArgumentParser()
parser.add_argument('args', help='Help', ...
0
votes
3
answers
78
views
have top-level parser and subparsers act on the same variable
i have an ArgumentParser with sub-parsers. Some flags are common for all sub-parsers, and I would like to be able to specify them either before or after the sub-command, or even mix before and after (...
0
votes
1
answer
49
views
Strange shell tab completion behaviour
I'm using the argparse Python module in a script that looks like this:
import argparse
def get_info(line: str) -> tuple:
print(line.split("|"))
def main():
p = argparse....
0
votes
2
answers
257
views
How to define separator characters for argparse?
Python's argparse module is a powerful tool for parsing script command line arguments. The problem is that it expects the args to be separated by spaces. Is there a way to let argparse know I am using ...
0
votes
0
answers
41
views
Argparse - Check if positional argument is entered
with argparse, is it possible to check whether the first positional argument has been entered:
import argparse
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser....