当前位置:网站首页>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.
边栏推荐
猜你喜欢

【NeRF】原始论文解读

Rust learning: 6.3_ Tuples of composite types

Relaxation class: the boss will martial arts, who also can not hold up against!The charm of six sigma training

Rust learning: 6.1_Slices of composite types

IDLE development wordCount program (5)

手把手教你进行Mysql查询操作

自动化测试框架Pytest(三)——自定义allure测试报告

每日一题,数组字符串的匹配问题

js reduce

神经网络样本太少怎么办,神经网络训练样本太少
随机推荐
明明加了唯一索引,为什么还是产生重复数据?
Using the color picker
Johnson全源最短路
.NET-8. My Thought Notes
并查集模板
幂函数 指数函数 对数函数
高性能短连接设计
34. 谈谈为什么要拆分数据库?有哪些方法?
概率分布及其应用
iwemeta metaverse: a doll sells for 9999 yuan, and Bubble Mart thinks it is not expensive at all
搭建 risc-v 编译环境
Day37 LeetCode
Introduction to C integer data storage
FFT模板
Synchronization lock synchronized traces the source
Summary of ctfshow SSTI knowledge points
自动化测试框架搭建 ---- 标记性能较差用例
LaTeX出现错误代码Command \algorithmic already defined
AFNetworking概述和4.0的实践
JS reduce