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 argparse required?

How can I make it a required argument only if I don't select another option, so when I select version it shouldn't require -f, but the rest of the time it should be required? parser = argparse.ArgumentParser( description="This script will…
benni347
  • 23
  • 2
1
vote
3 answers

Python argparse: How to make an initial parameter determine how many parameters come after?

I'd like to do the following, where the type specified dictates what parameters I can use after. For example, for cmd1, I'd expect a param1 and param2. For cmd2, I'd expect a param3, param4, param5, and param6. For cmd3, I'd expect no parameters. If…
trialUser
  • 13
  • 4
1
vote
1 answer

Debug a Python script in VSCODE while calling it from terminal with argparse

suppose I have the following script: import argparse parser = argparse.ArgumentParser() parser.add_argument('-t','--text', help="Input a text") args = parser.parse_args() def test_function(x): y = x print(y) if __name__…
Jean_N
  • 489
  • 1
  • 4
  • 19
1
vote
0 answers

Lost trying to use argparse

I'm creating a script to automate plethora of related tasks, however I'd like to be able to run specific parts of the script from command line, such as C:\> python main.py --debug (or just -d) Or something like C:\> python main.py --log (or just…
itzilly
  • 11
  • 2
1
vote
1 answer

How to unittest validation of arguments in argparse using assertRaises() in Python?

When trying to Unittest validations of arguments in argparse the following works: mymodule: def validate_mac_addr(mac_addr): regex = re.compile(r'^((([a-f0-9]{2}:){5})|(([a-f0-9]{2}-){5}))[a-f0-9]{2}$', re.IGNORECASE) if re.match(regex,…
1
vote
2 answers

Mutually exclusive group with a choice and optional

I am trying to achieve a command that has two 'modes' like this: cmd --list or cmd option --flag -abc In other words, either the user specifies the --list option or the parser 'chooses' the default path. The default path should be a choice from a…
Schottky
  • 1,549
  • 1
  • 4
  • 19
1
vote
1 answer

Issue with command prompt (windows) and python script after restarting script

I am trying to write a CLI program with python in windows. I recently came across an issue with argparse and my work-around was to clear the command prompt and re-run my script with the same arguments using this…
fwlaugh
  • 33
  • 5
1
vote
2 answers

Can argparse manage multiple sets of positional arguments?

I am wanting to handle input of multiple sets of data from the command line, and I'm not sure how to do it. I am thinking that having multiple optional arguments with multiple positional arguments would work, but I don't know how to do it. For…
PoDuck
  • 1,381
  • 15
  • 38
1
vote
2 answers

How to deal with different arguments that may have similarly named dest?

Let's say you want to use subcommands and at its core the subcommands want the same object data points to be stored in Namespace but perhaps grouped by subcommands. How can one extend argparse but not lose any of its standard behavior while…
LeanMan
  • 474
  • 1
  • 4
  • 18
1
vote
1 answer

How to change parser titles when using Argparse without modifying internal variables?

I'm using Python's argparse module to create a CLI for my application. I've made a subparsers variable to store the parsers for each command, but when I can't find a way to change the title of the subparsers without modifying parser's (the main…
Wizard28
  • 355
  • 1
  • 3
  • 12
1
vote
2 answers

Setting a function with a parser command

When I launch my code I would like to choose a function (from a set of functions) which will be used. Unfortunately I have many loops and the code is very expensive so the following pseudocode is is highly deprecated: import argparse import…
Siderius
  • 174
  • 2
  • 14
1
vote
2 answers

ValueError: Invalid file path or buffer object type:

Im new in python, need help in my code, please. I am trying to append data from multiple excel files to one file. But something goes wrong... import pandas as pd import argparse import os parser = argparse.ArgumentParser(description="extract data…
tampampam
  • 21
  • 1
  • 3
1
vote
1 answer

Multiple argument in python argparse

I want my script to take multiple argument with same name. python3 script.py --test test1 --test test2 --test test3 --config config_path How can I achieve this. I have tried nargs from argparse till now. # My Solution import argparse parser =…
Pranjal Doshi
  • 862
  • 11
  • 29
1
vote
1 answer

Can I direct Python's argparse to treat all arguments after a positional arg as also positional, even if they match an optional arg?

I am writing a utility (call it runner) which, as its main functionality, runs another script. That script to be run is passed as an argument, as well as any arguments that should be passed to the script. So, for example: runner script.py arg1…
1
vote
2 answers

Python Argparse: Get the command-line argument used for a Namespace variable

Is there a proper, or at least better, way to get which command-line argument was used to set an Namespace argument (attribute) value? I am currently using something like this: >>> import argparse >>> >>> parser = argparse.ArgumentParser() >>>…
Gabe
  • 131
  • 1
  • 13