Skip to content
Advertisement

Converting an argparse bash script into a python script

Consider the bash script below. The script argparse_test.py is an argparse script with various variables.

JavaScript

How would I convert this bash script to a python script so that I could run it in a python IDE with the various variables?

Advertisement

Answer

This bash input

JavaScript

should produce a sys.argv that looks like

JavaScript

that’s a rough guess; bash may be handling the slashes different (I’m on a windows machine now).

“running in an IDE” is a vague request.

Some IDE have a run menu item, and a way of specifying commandline arguments (usually in a separate menu setup item).

If you have code that setups up a parser:

JavaScript

the line

JavaScript

reads that sys.argv[1:] and parses it. So an alternative way to use the parser is to provide the equivalent list of strings directly:

JavaScript

If you can modify the script, you could add a couple of displays

JavaScript

and after the parse_args

JavaScript

args will be a argparse.Namespace object that should display like

JavaScript

is a dict with the same keys and values

Any python object with the same attributes should be have the same as args in the rest of your code.

I have a feeling this answer is too long and detailed – because of the vagueness of the question.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement