Skip to content Skip to sidebar Skip to footer

Very Basic Example Of Argparse?

am trying to make the most basic code that shows how arge parse works so I can understand it. I tried reading tutorials on argparse but its extremely confusing. I am just trying to

Solution 1:

here is a very simple example.

You have to save it as arg_parse.py and run it in your terminal with

python3 arg_parse.py -o hello_world

it will print hello world in your terminal enter image description here

CODE:

importargparseparser= argparse.ArgumentParser()

parser.add_argument('-o', "--opts",)    

args = parser.parse_args()
opts = args.opts
print(opts)

Explanation: It takes whatever your enter in the terminal after --opts (or short -o) and saves it to variable args.opts you can use it then as you would normally in python

Post a Comment for "Very Basic Example Of Argparse?"