命令行参数python sys模块_[Python小笔记]命令行参数:sys.argv和getopt模块

本文详细介绍了Python中的getopt模块,用于解析命令行选项和参数。getopt函数接受args、options和可选的long_options参数,返回一个包含(option, value)对的列表和剩余的非选项参数。支持短选项和长选项的混合使用,对于需要参数的选项,可以使用等号(=)指定。通过这个库,开发者可以方便地处理命令行输入。
摘要由CSDN通过智能技术生成

getopt(args, options[, long_options]) -> opts, args

Parses command line options and parameter list. args is the

argument list to be parsed, without the leading reference to the

running program. Typically, this means “sys.argv[1:]”. shortopts

is the string of option letters that the script wants to

recognize, with options that require an argument followed by a

colon (i.e., the same format that Unix getopt() uses). If

specified, longopts is a list of strings with the names of the

long options which should be supported. The leading ‘–’

characters should not be included in the option name. Options

which require an argument should be followed by an equal sign

(’=’).

The return value consists of two elements: the first is a list of

(option, value) pairs; the second is the list of program arguments

left after the option list was stripped (this is a trailing slice

of the first argument). Each option-and-value pair returned has

the option as its first element, prefixed with a hyphen (e.g.,

‘-x’), and the option argument as its second element, or an empty

string if the option has no argument. The options occur in the

list in the same order in which they were found, thus allowing

multiple occurrences. Long and short options may be mix

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值