argparse

How do I suppress an argument when nothing is input on command line?

How do I suppress an argument when nothing is input on command line? Question: #!/usr/bin/env python3 import argparse parser = argparse.ArgumentParser() parser.add_argument(‘–selection’, ‘-s’) parser.add_argument(‘–choice’, ‘-c’, default = argparse.SUPPRESS) args = parser.parse_args() def main(selection, choice): print(selection) print(choice) if __name__==’__main__’: main(args.selection, args.choice) The example provided is just to provide something simple and short that accurately articulates the …

Total answers: 1

How to parse a string in argparser during execution

How to parse a string in argparser during execution Question: I want to create a mini shell, so after i run my file, python3 ./main.py With whatever arguments I add, I then just constantly take input like a normal shell, >> command output >> -s output >> … So I am just doing simple while …

Total answers: 1

What is the best way to set up argparse for this conditional case of arguments?

How can I set up argparse to parse either 'diff -g file1 -r file2' or 'format file'? Question: I want to use argparse to parse command line arguments for my program. The program supports two modes – diff and format. When -diff is chosen, the arguments -g <file1> and -r <file2> are also needed. When …

Total answers: 2

Store argparse input to use as variable

Store argparse input to use as variable Question: I am using argparse to require input from the user for a hardware id to then be called later on, I cannot work out how to get it so the user types <command> –id <id> Please help me see where I’m going wrong! Thanks parser = argparse.ArgumentParser(description=’Return …

Total answers: 1

Argparse and unittest.main()

Argparse and unittest.main() Question: I just added a graph utility to a unittest — basically, the fully automatic version of the test just does a numerical compare, but I want a human to be able to ask for plots. Just using argparse, unittest.main() was choking if I used my new argument. What I’m currently doing …

Total answers: 1

Why does argparse not accept "–" as argument?

Why does argparse not accept "–" as argument? Question: My script takes -d, –delimiter as argument: parser.add_argument(‘-d’, ‘–delimiter’) but when I pass it — as delimiter, it is empty script.py –delimiter=’–‘ I know — is special in argument/parameter parsing, but I am using it in the form –option=’–‘ and quoted. Why does it not work? …

Total answers: 3

How to generate a html file from a markdown inputfile through command line argument

How to generate a html file from a markdown inputfile through command line argument Question: From the below part of a code I would like to generate an output in .html format from a given .md file through command line arguments #main.py import os, argparse, import configparser, webbrowser parser = argparse.ArgumentParser() parser.add_argument(‘–display’, dest=’display’,action=’store_true’, help=’displays the …

Total answers: 1

Why is argparse not recognizing my input file?

Why is argparse not recognizing my input file? Question: This is my first attempt at writing something with argparse and I am really lost. The goal of this script is to read file.sdf, then write it back out as file2.sdf This is my script: import argparse import rdkit as rdkit from rdkit.Chem import PandasTools import …

Total answers: 1

Python command parameter indicating time delta

Python command parameter indicating time delta Question: I need to specify a parameter to a python script indicating time back from now. For example 1d is 1 day from now, 2h is 2 hours from now, 2d3h is 2 days, 3 hours from now. Similar to the journalctl –vacuum-time format (reference). For example, this script …

Total answers: 2

Set constant for arg parse when using nargs '*'

Set constant for arg parse when using nargs '*' Question: I have a setup like this. What I want to do is to send a constant value if only the -e/–exp are sent, and if -p/–plot are sent then it should only do the plotting. So a default value will not work, as it then …

Total answers: 1