Python and Ruby command line parameter parsing

Python

optparse

# Python

from optparse import OptionParser

usage = "here is help messages of the command line tool."
opt_parser = OptionParser(usage)

opt_parser.add_option('-s', '--switch', action='store_false', help='set options as switch')
opt_parser.add_option('-n', '--name', type='string', dest='nickname', help='pass in single name')
opt_parser.add_option("-f", "--filename", metavar="FILE", help="write output to FILE")

test_args = ['-n', 'looking', '-s', '-f', 'outfile.txt']
# python test.py -n looking -s -f outfile.txt
(options, args) = opt_parser.parse_args(test_args)
print(opt_parser.print_help())
print(options)
[root@master python3_learning]# python3 test.py
Usage: here is help messages of the command line tool.

Options:
  -h, --help            show this help message and exit
  -s, --switch          set options as switch
  -n NICKNAME, --name=NICKNAME
                        pass in single name
  -f FILE, --filename=FILE
                        write output to FILE
None
{'switch': False, 'nickname': 'looking', 'filename': 'outfile.txt'}

1. The -arg1 and --arg2 in the add_option option generally correspond, and the parsing result will be saved in args2 by default, such as the -f option in the following example:

opt_parser.add_option("-f", "--filename", metavar="FILE", help="write output to FILE")

Finally, the corresponding analysis result will be saved in filename: 

{..., 'filename': 'outfile.txt'}

2. Unless you specify a specific dest='args3', it will be saved with args3 as the variable name. For example, the -n option in the following example:

opt_parser.add_option('-n', '--name', type='string', dest='nickname', help='pass in single name')

Finally, the corresponding parse result will be saved as nickname (not name) (if there is no dest='nickname' option, the result will contain'name':'looking'): 

{..., 'nickname': 'looking', ...}

3. action='store_true' (action='store_false') means that the user does not need to give a parameter value. This type defines the Boolean value true (false) to be saved to the variable specified by dest, such as the -s option in the following example:

opt_parser.add_option('-s', '--switch', action='store_false', help='set options as switch')

 The result of parsing -s is (if there is no -s in the option, the result is'switch': None):

{'switch': False, ...}

4. The default parameter in the add_option option is used to set the default value:

opt_parser.add_option("-u", "--user", default="root", help="set user to login")

 If the value of -u is not specified in the parameter, the parsing result will use the default value:

{..., 'user': 'root'}

argparse

It is said that argparse is more powerful than optparse. The following example shows how to use multiple sub-parsers, so I don’t need to talk about the details.

# python
# command.py

import argparse



def run_scan(args):
    print('run_scan', args.p, args.l)


def run_aparser(args):
    print('run_aparser', args.p)


class Command:

    def __init__(self):
        self.parser = argparse.ArgumentParser(description='x2openEuler tool chain')
        self.subparser = self.parser.add_subparsers(description='subcommand parser')
        self.scan_parser = self.subparser.add_parser('scan', help='Migration assessment tool')
        self.aparser = self.subparser.add_parser('collect-conf-info', help='aparser tool')
        self.add_args()
        self.set_func()

    def add_args(self):
        self.parser.add_argument('-v', '--version', action='version', version='0.1.0 beta', help='Display version')
        # add_argument 中参数不加 - 的话表示位置参数,
        self.scan_parser.add_argument('p', help='Specify the path of scan')
        self.scan_parser.add_argument('-l', choices=['auto', 'c', 'python', 'java'], help='Select the language of scan')
        self.aparser.add_argument('p', help='Specify the path of file_dir')

    def set_func(self):
        # python3 command.py scan /root/lukaiyi/ttt/python3-pandas-0.25.3-1.oe1.aarch64.rpm -l python
        self.scan_parser.set_defaults(func=run_scan)
        # python3 command.py collect-conf-info /root/lukaiyi/ttt/test
        self.aparser.set_defaults(func=run_aparser)

    def exec_func(self):
        args = self.parser.parse_args()
        args.func(args)


if __name__ == '__main__':
    Command().exec_func()

Later, I found out that this nargs is really easy to use (others have not been studied carefully yet), and it can set default values ​​for positional parameters (very awkward, a bit subverted my cognition, I thought it was Optional parameters can be set to default values) .

When nargs is not set, it looks like this:

from argparse import ArgumentParser

parser = ArgumentParser(description='Test')
parser.add_argument("command", help="the command to be executed",
                    choices=["dump", "delete", "update", "set"], default="set")
args = parser.parse_args()
print(args.command)
C:\Users\lukaiyi\insight-tool>python test.py delete
delete

C:\Users\lukaiyi\insight-tool>python test.py
usage: test.py [-h] {dump,delete,update,set}
test.py: error: the following arguments are required: command

What I originally thought was that if the positional parameters were not written, the default value set would be returned to me, but the result did not give me face.

But adding nargs='?' is not the same (even if the positional parameter is not written at this time, you can directly use the given default value):

nargs = '?' 

from argparse import ArgumentParser

parser = ArgumentParser(description='Test')
parser.add_argument("command", help="the command to be executed",
                    choices=["dump", "delete", "update", "set"], nargs='?', default="set")
args = parser.parse_args()
print(args.command)
C:\Users\lukaiyi\insight-tool>python test.py
set

C:\Users\lukaiyi\insight-tool>python test.py delete
delete

C:\Users\lukaiyi\insight-tool>python test.py delete update
usage: test.py [-h] [{dump,delete,update,set}]
test.py: error: unrecognized arguments: update

There are also those that use  nargs='*'  and  nargs='+'  , which look similar to? * + In regular expressions (in fact, the meaning is indeed similar). 

'?' nargs = parameter indicates that the position can be set zero or one; nargs = '*' represents a position of the parameter can be set to zero or more; nargs = '+' indicates that the position parameter can be provided with a one or more. If it is still unclear, you can take a look at the following example.

nargs = '*'

from argparse import ArgumentParser

parser = ArgumentParser(description='Test')
parser.add_argument("command", help="the command to be executed",
                    choices=["dump", "delete", "update", "set"], nargs='*', default="set")
args = parser.parse_args()
print(args.command)
C:\Users\lukaiyi\insight-tool>python test.py
set

C:\Users\lukaiyi\insight-tool>python test.py delete
['delete']

C:\Users\lukaiyi\insight-tool>python test.py delete update
['delete', 'update']

 nargs = '+'

from argparse import ArgumentParser

parser = ArgumentParser(description='Test')
parser.add_argument("command", help="the command to be executed",
                    choices=["dump", "delete", "update", "set"], nargs='+', default="set")
args = parser.parse_args()
print(args.command)
C:\Users\lukaiyi\insight-tool>python test.py
usage: test.py [-h] {dump,delete,update,set} [{dump,delete,update,set} ...]
test.py: error: the following arguments are required: command

C:\Users\lukaiyi\insight-tool>python test.py delete
['delete']

C:\Users\lukaiyi\insight-tool>python test.py delete update
['delete', 'update']

Ruby

Ruby's parsing is even simpler, and you can understand it at a glance, not to mention more.

# Ruby
require 'optparse'

options = {}
opt_parser = OptionParser.new do |opts|
  opts.banner = 'here is help messages of the command line tool.'

  opts.separator ''
  opts.separator 'Specific options:'
  opts.separator ''

  options[:switch] = false

  opts.on('-s', '--switch', 'Set options as switch') do
    options[:switch] = true
  end

  opts.on('-n NAME', '--name Name', 'Pass-in single name') do |value|
    options[:name] = value
  end

  opts.on('-a A,B', '--array A,B', Array, 'List of arguments') do |value|
    options[:array] = value
  end

  opts.on_tail('-h', '--help', 'Show this message') do
    puts opts
    exit
  end
end

opt_parser.parse!(ARGV)
puts options.inspect
[root@master ruby_learning]# ./test.rb -h
here is help messages of the command line tool.

Specific options:

    -s, --switch                     Set options as switch
    -n, --name Name                  Pass-in single name
    -a, --array A,B                  List of arguments
    -h, --help                       Show this message

[root@master ruby_learning]# ./test.rb -a hello,world
{:switch=>false, :array=>["hello", "world"]}

[root@master ruby_learning]# ./test.rb -s -a hello,world
{:switch=>true, :array=>["hello", "world"]}

[root@master ruby_learning]# ./test.rb -s -n looking -a hello,world
{:switch=>true, :name=>"looking", :array=>["hello", "world"]}

 

Guess you like

Origin blog.csdn.net/TomorrowAndTuture/article/details/110684666