Fix Python – Python argparse: Make at least one argument required

I’ve been using argparse for a Python program that can -process, -upload or both:
parser = argparse.ArgumentParser(description=’Log archiver arguments.’)
parser.add_argument(‘-process’, action=’store_true’)
parser.add_argument(‘-upload’, action=’store_true’)
args = parser.parse_args()

The program is meaningless without at least one parameter. Ho….

Fix Python – Using the same option multiple times in Python’s argparse

I’m trying to write a script that accepts multiple input sources and does something to each one. Something like this
./my_script.py \
-i input1_url input1_name input1_other_var \
-i input2_url input2_name input2_other_var \
-i input3_url input3_name
# notice inputX_other_var is optional

But I can’t quite figure out how to do this usin….

Fix Python – Argparse: Required argument ‘y’ if ‘x’ is present

I have a requirement as follows:
./xyifier –prox –lport lport –rport rport

for the argument prox , I use action=’store_true’ to check if it is present or not.
I do not require any of the arguments. But, if –prox is set I require rport and lport as well. Is there an easy way of doing this with argparse without writing custom conditional codin….

Fix Python – Check if argparse optional argument is set or not

I would like to check whether an optional argparse argument has been set by the user or not.
Can I safely check using isset?
Something like this:
if(isset(args.myArg)):
#do something
else:
#do something else

Does this work the same for float / int / string type arguments?
I could set a default parameter and check it (e.g., set myArg = -1,….

Fix Python – Get selected subcommand with argparse

When I use subcommands with python argparse, I can get the selected arguments.
parser = argparse.ArgumentParser()
parser.add_argument(‘-g’, ‘–global’)
subparsers = parser.add_subparsers()
foo_parser = subparsers.add_parser(‘foo’)
foo_parser.add_argument(‘-c’, ‘–count’)
bar_parser = subparsers.add_parser(‘bar’)
args = parser.parse_args([‘-g’, ….

Fix Python – In Python, using argparse, allow only positive integers

The title pretty much summarizes what I’d like to have happen.
Here is what I have, and while the program doesn’t blow up on a nonpositive integer, I want the user to be informed that a nonpositive integer is basically nonsense.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(“-g”, “–games”, type=int, default=162,
….