The use of argparse module in python

1. argparse module

    argsparse is a standard module for python's command line parsing. It is built into python and does not need to be installed. This library allows us to pass parameters to the program directly on the command line and let the program run.

2. Use of argparse module

import argparseImport the module     first in the code .
    The use process mainly includes the following 3 steps:

  • Create an ArgumentParser() object
  • Call the add_argument() method to add parameters
  • Use parse_args() to parse the added arguments

    The specific example code is as follows:

import argparse
 
parser = argparse.ArgumentParser()
parser.add_argument('--sparse', action='store_true', default=False, help='GAT with sparse version or not.')
parser.add_argument('--seed', type=int, default=72, help='Random seed.')
parser.add_argument('--epochs', type=int, default=10000, help='Number of epochs to train.')
 
args = parser.parse_args()
 
print(args.sparse)
print(args.seed)
print(args.epochs)

    The output of running the above code is as follows:

/home/user/anaconda3/bin/python3.6 /home/user/lly/pyGAT-master/test.py
False
72
10000
 
Process finished with exit code 0

    The argparse.add_argument() method defines how to parse command-line arguments:

1
ArgumentParser.add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])

    The basic parameters of the add_argument function are as follows:
insert image description here

3. Reference blog post

    [1] How to use add_argument() .
    [2] Detailed explanation of argparse module usage examples .

Guess you like

Origin blog.csdn.net/weixin_43981621/article/details/121684241