python argparse例子

简介:
  1. arg_1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/python
#coding=utf-8
import  argparse
def  parse_args():
     description  =  "usage: % prog[options]"
     parser  =  argparse.ArgumentParser(description = description)
     parser.add_argument( 'addresses' , nargs = '*' help = 'help' )
     parser.add_argument( 'filename' help = 'help' )
     parser.add_argument( '-p' '--port' type = int help = 'help' )
     parser.add_argument( '--iface' help = help , default = 'localhost' )
     parser.add_argument( '--delay' type = float help = help , default = . 7 )
     parser.add_argument( '--bytes' type = int help = help , default = 10 )
 
     args  =  parser.parse_args()
     return  args
 
if  __name__  = =  '__main__' :
     args  =  parse_args()
 
     for  address  in  args.addresses:
         print  'The address is : %s .'  %  address
         print  'The filename is : %s .'  %  args.filename
         print  'The port is : %d.'  %  args.port
         print  'The interface is : %s.'  %  args.iface
         print  'The number of seconds between sending bytes : %f'  %  args.delay
         print  'The number of bytes to send at a time : %d.'  %  args.bytes
 
#-p 22 --delay 1.2 127.0.0.1 172.16.55.67 poetry/ecstasy.txt


2.arg_2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/python
# coding=utf-8
import  argparse
from  argparse  import  ArgumentParser, RawTextHelpFormatter
 
def  get_args():
     """实例化类,formatter_class参数允许help信息以自定义的格式显示"""
     parser  =  ArgumentParser(description = "This is a tool for execute command(s) on remote server(s) or get/put file(s) from/to the remote server(s)\nNotice: please always use '/' as path separater!!!" ,formatter_class = RawTextHelpFormatter,epilog = "Notice:\n  If any options use more than once,the last one will overwrite the previous" )
     remote_command  =  parser.add_argument_group( 'remote command' 'options for running remote command' )
     remote_command.add_argument( '--cmd' , metavar = '“COMMAND”' , dest = 'cmd' , help = "command run on remote server,multiple commands sperate by ';'" )
     sftp  =  parser.add_argument_group( 'sftp' 'options for running sftp' )
     sftp.add_argument( '--put' , metavar = '',  help = "transfer from local to remote" , nargs = 2 )
     sftp.add_argument( '--get' , metavar = '',  help = "transfer from remote to local" , nargs = 2 )
     global  args
     args  =  vars (parser.parse_args())
     print  args
     =  0
     for  in  ( 'cmd' 'put' 'get' ):
         if  in  args:
             if  args[i]  is  None :
                 del  args[i]
             else :
                 + =  1
     if  n >  1 :
         print ( '\n  Only one of the "--cmd --put --get" can be used!' )
         exit( 10 )
 
if  __name__  = =  '__main__' :
     get_args()
     if  'cmd'  in  args:
         echo_cmd  =  args[ 'cmd' ]
         print  echo_cmd
         
  # --cmd uptime --get /home/nginx /home/nginx



本文转自 liqius 51CTO博客,原文链接:http://blog.51cto.com/szgb17/2055878,如需转载请自行联系原作者

相关文章
|
12月前
|
存储 Python
30天拿下Python之argparse模块
30天拿下Python之argparse模块
88 0
|
Python
【Python3】argparse解析与使用_python3 argparse
【8月更文挑战第1天】argparse 是对终端输入的命令行的参数进行解析,俗称命令行解析器。
111 1
|
测试技术 Python
python 命令行参数 argparse详解
这篇文章是关于Python命令行参数解析库`argparse`的详细解释和使用教程。文中提供了完整的代码模板,包括如何定义命令行参数、如何添加位置参数、可选参数和布尔参数,以及如何解析和使用这些参数。文章还包含了示例代码和测试用例,以展示如何在实际程序中应用`argparse`库。
182 0
|
Python
python命令行解析模块argparse
python命令行解析模块argparse
|
存储 开发者 Python
Python中的argparse模块:命令行参数解析的利器
Python中的argparse模块:命令行参数解析的利器
152 2
|
UED 开发者 Python
使用Python构建命令行工具:argparse和click库的使用
使用Python构建命令行工具:argparse和click库的使用
382 0
|
JSON 编解码 Linux
Python笔记1(赋值、浅拷贝和深拷贝、字符串日期转换、argparse、sys、overwrite、eval、json.dumps/json.loads、os.system(cmd)、zfill)
Python笔记1(赋值、浅拷贝和深拷贝、字符串日期转换、argparse、sys、overwrite、eval、json.dumps/json.loads、os.system(cmd)、zfill)
183 0
Python笔记1(赋值、浅拷贝和深拷贝、字符串日期转换、argparse、sys、overwrite、eval、json.dumps/json.loads、os.system(cmd)、zfill)
|
存储 开发工具 开发者
命令行参数解析神器:深入剖析Python中的argparse模块
命令行参数解析神器:深入剖析Python中的argparse模块
|
Python 容器
|
存储 Python
【Python标准库】argparse——命令行选项、参数和子命令解析器
【Python标准库】argparse——命令行选项、参数和子命令解析器

热门文章

最新文章

推荐镜像

更多