当前位置:网站首页>Different command line styles
Different command line styles
2022-08-10 08:16:00 【Dancen】
一. Introduction to the command line format
Recently plan to write a command line parser,It is known that the writing format of the command line is relatively free,例如:
ls -l -a
ls -la
ls -l –all
以上3The purpose of the commands is exactly the same,Both list the detailed information of all files in the current directory,But the spelling is very different,How to parse it.
为了做到这一点,It is necessary to systematically understand the format standard of the command line,The general format of the command line is:
命令 [选项1] [选项2] [选项2参数] [命令参数]
The following command
du -h --max-depth 1 /home
为例,explain the concept.
1. 命令
如示例中的du,command is the calling name of the program.
2. 选项、选项参数、命令参数
对于程序来说,Everything after the command is programmatic“参数”,Can be recognized and analyzed by the program itself,并无本质不同.
但具体而言,又可以把“参数”分为3类:选项、选项参数、命令参数.
2.1. 选项
如示例中的-h、--max-depth,option is a special program“参数”,Options by the procedure defined in advance,是固定的,是常量.
2.2. 选项参数
如示例中的1,it belongs to options--max-depth的值,it's relatively fixed,The range specified by the corresponding options of the program needs to be met,If must be an integer.
2.3. 命令参数
如示例中的/home,The command parameter is the execution object of the program,相对自由.
二. Different styles of command line
In order to distinguish options in the command line、选项参数、命令参数,需要制定一些规则.然而,在实践中,Due to the lack of uniform norms,Created a different command line style.
1. UNIX/POSIX风格
Unixstyle command line,选项是以连字符“-”开头的单个字符(注意是一个字符,而不是一个单词),short option.
如果选项后面不带参数,then such an option is called a mode option.模式选项可以组合在一起使用.
例如:
mycommand -a -b
如果-a和-bis the mode option,Then it can be combined as-ab或-ba:
mycommand -ab
mycommand -ba
If option takes arguments,These parameters should come immediately after the options,can be separated by spaces,can also not be separated by spaces.
例如:
mycommand -p 9002
mycommand -p9002
mycommand -p9002Can it be understood as a combination of multiple options??表面上来看,Is the existence of ambiguity,具体上来说,Since options are fixed,Then the program can identify by itself whether this is an option merging,or a combination of options and options arguments.
Due to the use of a single character as an option,This limits the range of options,If the command is very complex,Options may not be enough,并且难以理解.
2. GNU风格
GNUstyle command line,选项是以“--”开头的一个单词,long option.
例如:
mycommand --verbose.
If options require parameters,then use a space or“=”Separate parameters and options.
例如:
mycommand --port 9501
mycommand --port=9501
GNUstyle to avoidUNIXflaws in style,Although not so concise,This is also the recommended command line style.在实践中,Often a combination of the two styles is used.
3. BSD风格
和UNIX/POSIX风格相比,Its options use a single character,without any prefix.
例如:
mycommand a b c
If multiple options without arguments,也可以组合在一起.
例如:
mycommand abc
If options require parameters,Option arguments immediately follow the option,可以使用空格分开,You can also use no spaces.
例如:
mycommand P 9501
mycommand P9501
This style is the most concise,但问题也最多,Difficulty distinguishing options without understanding、选项参数、命令参数,I think it's rubbish.
4. X toolkit风格
原始的X toolkitstyled byX toolkit进行解析,Its options are“-”开头的一个单词,It first filter and deal with some options(-display)Then pass the filtered command line to the application logic for parsing.
例如:
mycommand -version
If options require parameters,then the option argument immediately follows the option,and separated by spaces.
例如:
mycommand -port 9501
5. DOS风格
The options are“/”开头的单个字符.
例如:
mycommand /C /D
Options can be combined.
例如:
mycommand /C/D
If option takes arguments,then the option parameter immediately follows the option,Do not separate with spaces.
例如:
mycommand /CP/D
三. 命令行解析
The core problem of command line parsing is how to identify options、选项参数、命令参数.Since command line writing is very free,This makes it difficult to parse the command line.
1. Indicators for stop scanning options and option parameters
Take the following command line as an example:
mycommand -a p1 --list 9501 test
可以认为9501是选项--list的选项参数;
也可以认为9501是命令参数,即选项--list不带参数.
这就产生了歧义.实践中,In this case, a method called“scan stop”sign to avoid ambiguity.
例如:
mycommand -a p1 --list -- 9501 test
“--”is an indicator,Show that stop the scanning parameters,All parts after that are command parameters,instead of options or option arguments.
2. Conflicts between different command line styles
Given the freedom of the command line writing format,There are various other command line styles.As far as several command line styles are known to be popular,There have been conflicting parts.
例如:
mycommand -abc
mycommand -abc
以UNIX/POSIXstyle to parse,命令行带有3个参数,而以X toolkitstyle to parse,then the command line has only one parameter.
因此,Parsing the command line with general rules is not possible.
边栏推荐
- 详解构建mock服务最方便的神器——Moco
- day16--抓包工具Charles的使用
- 【Rust指南】使用Cargo工具高效创建Rust项目 | 理解Rust特别的输入输出语句
- [深入研究4G/5G/6G专题-56]: L3信令控制-5-无线承载管理
- AFNetworking概述和4.0的实践
- 预测股票涨跌看什么指标,如何预测明天股票走势
- iwemeta metaverse: a doll sells for 9999 yuan, and Bubble Mart thinks it is not expensive at all
- 人工神经网络模型的特点,人工神经网络模型定义
- 短视频同城流量宣传小魔推有何优势?如何给实体商家带来销量?
- 34. Talk about why you want to split the database?What methods are there?
猜你喜欢
WooCommerce installation and rest api usage
自动化测试框架Pytest(二)——前后置处理
2022-08-01 Advanced Network Engineering (24) STP Advanced Knowledge
快速输入当前日期与时间
34. Talk about why you want to split the database?What methods are there?
【NeRF】原始论文解读
明明加了唯一索引,为什么还是产生重复数据?
iwemeta元宇宙:一个娃娃卖9999元,泡泡玛特认为一点也不贵
Rust learning: 6.1_Slices of composite types
Solve the problem that the win10win7win8 system cannot find the specified module and cannot register the desert plug-in
随机推荐
2022-08-01 Advanced Network Engineering (24) STP Advanced Knowledge
组合数模板
Go-Excelize API源码阅读(十一)—— GetActiveSheetIndex()
Obtain - 65 [chances] : "soldiers, subtlety also - 7-36 meter reading - defeat
高性能短连接设计
34. 谈谈为什么要拆分数据库?有哪些方法?
2022-08-01 网工进阶(二十四) STP进阶知识
What is an MQTT gateway?What is the difference with traditional DTU?
PLSQL学习第四天
本地生活商家如何通过短视频赛道,提升销量曝光量?
关于数据中心的设计方案,数据中心网络规划设计
.NET-7.WPF learning experience summary
自动化测试框架Pytest(二)——前后置处理
模糊查询除了like+ % 还能用什么方式
JS reduce
nrm 使用详解
并查集模板
PLSQL学习第二天
Based on STC8G2K64S4 single-chip microcomputer to display analog photosensitive analog value through OLED screen
Quickly enter the current date and time