当前位置:网站首页>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.
边栏推荐
- .NET-8.我的思想笔记
- 【Rust指南】使用Cargo工具高效创建Rust项目 | 理解Rust特别的输入输出语句
- AFNetworking概述和4.0的实践
- 大体来讲,网站会被攻击分为几种原因
- Power function Exponential function Logarithmic function
- m.bjhjwy.com全面教学设备 类型包括: 教学仪器, 教学设备 ,
- 不同的命令行风格
- NPU架构与算力分析
- Obtain - 65 [chances] : "soldiers, subtlety also - 7-36 meter reading - defeat
- 90. (cesium house) cesium height monitoring events
猜你喜欢
MySQL设置初始密码—注意版本mysql-8.0.30
探索神经网络架构教程视频,设计神经网络的步骤
自动化测试框架Pytest(二)——前后置处理
nrm 使用详解
2022-08-01 Advanced Network Engineering (23) Advanced VLAN Technology - VLAN Aggregation, MUX VLAN
day16--The use of the packet capture tool Charles
时序动作定位 | ASM-Loc:弱监督时序动作定位的动作感知片段建模(CVPR 2022)
CV+Deep Learning - network architecture Pytorch recurrence series - classification (3: MobileNet, ShuffleNet)
二叉树 --- 堆
90. (cesium house) cesium height monitoring events
随机推荐
foreach遍历删除元素问题总结
Introduction to the delta method
nrm 使用详解
WooCommerce installation and rest api usage
11111
.NET-8.我的思想笔记
如何设计神经网络结构,神经网络设计与实现
navicat for mysql 连接时报错:1251-Client does not support authentication protocol requested by server
The precise effect of network integration promotion outsourcing in the era of Internet of Things-Shenzhen Win-Win World News
PLSQL学习第二天
【Unity入门计划】制作RubyAdventure03-使用碰撞体&触发器实现世界交互
自动化测试框架Pytest(三)——自定义allure测试报告
Delphi实现的一个文件在线查询显示下载功能
短视频同城流量宣传小魔推有何优势?如何给实体商家带来销量?
自动化测试框架Pytest(一)——入门
Power function Exponential function Logarithmic function
Rust learning: 6.1_Slices of composite types
Go-Excelize API source code reading (11) - GetActiveSheetIndex()
问下cdc mysql to doris.不显示具体行数,怎么办?
Summary of ctfshow SSTI knowledge points