打印用法时如何表示命令行参数是可选的 [英] How to denote that a command line argument is optional when printing usage

查看:21
本文介绍了打印用法时如何表示命令行参数是可选的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个可以通过以下任一方式运行的脚本.

./foo arg1 arg2./foo

在打印命令的正确用法时,是否有一种普遍接受的方式来表示 arg1 和 arg2 不是强制参数?

我有时注意到使用方括号括起来的参数打印的用法,如下面的用法打印输出所示.

用法:./foo [arg1] [arg2]

这些括号是表示参数是可选的还是有另一种普遍接受的方式来表示参数是可选的?

解决方案

我个人还没有看到表示开关是可选的标准"(例如如何定义某些语言的编写方式的标准),因为这确实是个人选择,但根据 IBM 的文档Wiki 以及我个人见过的大量 shell 脚本(以及来自各种程序的命令行选项)和 IEEE,事实上"是将方括号 ([]) 参数视为可选参数.来自 Linux 的示例:

ping(输出修剪...)

用法:ping [-c count] [-t ttl] 主机

其中 [-c count][-t ttl] 是可选参数,但 host 不是(在帮助中定义).

我个人也遵循事实,使用 [] 表示它们是可选参数,并确保在使用该脚本/程序时注意这一点.

我应该注意到,计算机标准应该定义事情如何发生及其失败路径(真正的失败或未定义的行为).类似于命令行解释器_shall_将参数括在方括号中时视为可选参数,_shall_将X视为Y当Z等.很像 ISO C 标准规定的函数如何形成以使其有效(否则将失败).鉴于没有命令行解释器,从 ASH 到 ZSH 以及介于两者之间的所有内容,使将 [] 视为非可选的脚本失败,可以说没有真正的标准.>

Assume that I have a script that can be run in either of the following ways.

./foo arg1 arg2
./foo

Is there a generally accepted way to denote that arg1 and arg2 aren't mandatory arguments when printing the correct usage of the command?

I've sometimes noticed usage printed with the arguments wrapped in brackets like in the following usage printout.

Usage: ./foo [arg1] [arg2]

Do these brackets mean that the argument is optional or is there another generally accepted way to denote that an argument is optional?

解决方案

I personally have not seen a 'standard' that denotes that a switch is optional (like how there's a standard that defines how certain languages are written for example), as it really is personal choice, but according to IBM's docs and the Wiki, along with numerous shell scripts I've personally seen (and command line options from various programs), and the IEEE, the 'defacto' is to treat square bracketed ([]) parameters as optional parameters. Example from Linux:

ping (output trimmed...)

usage: ping [-c count] [-t ttl] host

where [-c count] and [-t ttl] are optional parameters but host is not (as defined in the help).

I personally follow the defacto as well by using [] to mean they are optional parameters and make sure to note that in the usage of that script/program.

I should note that a computer standard should define how something happens and its failure paths (either true fail or undefined behavior). Something along the lines of the command line interpreter _shall_ treat arguments as optional when enclosed in square brackets, and _shall_ treat X as Y when Z, etc.. Much like the ISO C standard says how a function shall be formed for it to be valid (otherwise it fails). Given that there are no command line interpreters, from ASH to ZSH and everything in between, that fail a script for treating [] as anything but optional, one could say there is not a true standard.

这篇关于打印用法时如何表示命令行参数是可选的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆