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

Argparse Unit Test with IP Address Module

I'd like to write unit tests for the following function: #!/usr/bin/env python3 """IPv4 validation using `ipaddress module` and argparse.""" import argparse from ipaddress import ip_address def parse_cli_args(): """ Command line parser…
marshki
  • 175
  • 1
  • 11
1
vote
1 answer

How to pass date range argument in SQL query with argparse Python

I have 3 arguments date, Name and country, this code allows to pass my optionals arguments for my SQL query, for example : import argparse parser = argparse.ArgumentParser() # provide a clear definition of possible…
Flora8832
  • 57
  • 5
1
vote
2 answers

Argparse tutorial example doesn't work in windows command line

So I'm trying to teach myself how to use the python library argparse via the tutorial here. The example is given by the following piece of code which is saved as tut.py. import argparse parser = argparse.ArgumentParser(description='Process some…
Christian Singer
  • 295
  • 3
  • 5
  • 15
1
vote
0 answers

Using `argparse` to handle arguments when it is already defined in a super class

I am using the Alexa Gadget Toolkit Python package to create an IoT device. (https://github.com/alexa/Alexa-Gadgets-Raspberry-Pi-Samples/tree/master/src/agt) Following from Amazon's example scripts, the IoT class is defined and executed as: from agt…
Jamie Poole
  • 508
  • 6
  • 17
1
vote
2 answers

Argparse: set default text file

I am looking to set a text file rand.letter.txt as the default word file in this argparse segment, to be used and read in the program. I would assume this should be situated in row 4-5 down below. How should I do this? As you will see I have already…
Becky8
  • 25
  • 4
1
vote
3 answers

Allowing Python Script to run even on --help argument in argparse

I am using Argparse module in python for developing a Command Line Tool. Here's the parser code: from argparse import ArgumentParser def arguments(): parser = ArgumentParser() parser.add_argument('-c' , '--comms' , action = "store" ,…
Aniruddh Anna
  • 43
  • 1
  • 8
1
vote
0 answers

python argparse having multiple subparsers in one commandline

I am trying to parse multiple groups of arguments in one command line. both setup1 and setup2 can have identical arguments python3 setup1 --param1 1 --param2 0 setup2 --param1 0 --param2 -1 I tried to use the subparse as below, but it appears it…
brane
  • 585
  • 6
  • 20
1
vote
1 answer

How to filter a list of an nargs argument in argparse?

Let's imaging I have a CLI with an nargs="*" argument called --list. I would like to filter out certain elements of the this list. For instance, if the user passes in --list foo bar foo baz I would like to filter out all values == "foo" so that the…
bluenote10
  • 23,414
  • 14
  • 122
  • 178
1
vote
1 answer

How to make a python script be stringent on checking parameters passed in?

I have the following script which simply parses arguments passed in commandline import argparse if __name__ == "__main__": parser = argparse.ArgumentParser(description='My test script') parser.add_argument('-somearg', dest='somearg',…
TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
1
vote
2 answers

How can I use a positional argument with a space in it in Pythonbrew?

I have been using the argparse module for accepting input into a program, with much success. I have defined about six optional arguments (arguments that need to be prepended with a -- can be placed anywhere in the list) and one positional argument…
Simon Hova
  • 293
  • 2
  • 12
1
vote
2 answers

Python argparse with changing the parameters number

I am trying to create a simple python with argparse flags. There are 2 arguments A and B and in case in aargument A the parameter that was entered was A it will ask for the 3rd parameter C. if in the first parameter A wasn't submitted it won't need…
1
vote
3 answers

How to make Shell script style arguments passing with argparse in Python

I have a python parsing arguments like below: Code: if __name__ == "__main__": parser = argparse.ArgumentParser(description='Args test') parser.add_argument('-myarg1', '--myarg1', type=str, dest='myarg1', required=True, help='Help…
AdeleGoldberg
  • 1,289
  • 3
  • 12
  • 28
1
vote
0 answers

How to set an argument group inside a mutually exclusive argument group with argparse?

How do I set the arguments so that I get a mutually exclusive group, consisting of an '--more-info' parameter and a group of 3 other required params like '--arg1', '--arg2', '--arg3'? In other words, how do I get this? script.py [--more-info]…
Sohail Saha
  • 483
  • 7
  • 17
1
vote
0 answers

Good practice for defining variables for paths for entire python package interactively via a script

So I'm not really sure how to do this and if this is a good idea at all. But I have a directory of some python modules and they all have references in there to some specific directories that get created while the sequential execution of one python…
Lenn
  • 1,283
  • 7
  • 20
1
vote
2 answers

Command line args accepting -SS also where as expected is -S using argparse

I have command line argument -S as defined below import argparse parser = argparse.ArgumentParser() parser.add_argument( '-S', '--save', action='store_true', help='Save to directory' ) but if I use -SS instead of…
pjk
  • 547
  • 3
  • 14