Python argparse notes

Here is some notes for using python argparse package,

This is an example with -f, -o options, the action is a simple store

import argparse

def parseArgs():
    parser = argparse.ArgumentParser(description='compiling graphit files')
    parser.add_argument('-f', dest = 'input_file_name')
    parser.add_argument('-o', dest = 'output_file_name')
    args = parser.parse_args(['-f', 'input'])
    return vars(args)

if __name__ == '__main__':
    args = parseArgs()
    input_file = args['input_file_name']
    output_file = args['input_file_name']

 

The full documentation is here

https://docs.python.org/3/library/argparse.html

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s