添加切换到命令行参数 [英] Adding switches to command line arguments

查看:168
本文介绍了添加切换到命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建下列方式我自己的命令,我想从两个批次和CMD.EXE运行:

 假命令-name<有的价值> -age<有的价值与GT;

目前我知道如下创建命令:

 假-命令<一些价值> <另一种价值与GT;

之后,我可以收集输入为%1 %2 。但是,这是要做到这一点,因为如果在我期待的输入顺序被改变,名称之前有人进入的年龄发生了什么没有有效的方法。

所以,我有两个问题:


  1. 如何像Windows命令行开关创建的Linux?

  2. 是我的做法正确吗?有没有更好的方式来做到这一点?


解决方案

@ECHO OFF
SETLOCAL
SET开关=名字年龄无所谓
SETnoargs =选项anotheroption
SETswindicators = / -
SETotherparameters =
::明确开关
FOR %%一个IN(%交换机%% noargs%)DO SET一%% =:swloop
IF%〜1==GOTO过程FOR %%一个IN(%swindicators%)DO(
 FOR %% B IN(%noargs%)DO(
  IF / I%%一个%% B==%〜1SET%% B = Y&放大器;移和放大器; GOTO swloop
 )
 FOR %% B IN(%开关%)DO(
  IF / I%%一个%% B==%〜1SET%% B =%〜2&安​​培;移和放大器;移和放大器; GOTO swloop
 )

SETotherparameters =%otherparameters%%1
转移
GOTO swloop:处理FOR %%一个IN(%交换机%% noargs%otherparameters)DO如果定义%% A(CALL ECHO %% A = %%%%一个%%)ELSE(ECHO %%一个没有定义)GOTO:EOF

下面是一个示范。

由于你有3个交换机的名称 年龄 任何其中接受一个参数和两个真正的开关,选项 anotheroption 不采用任何参数,那么在运行上述过程为

q27497516 -age 92 /选项goosefeathers /名称弗雷德搜索

(q27497516.bat是我batchname这里)将产生

  NAME =弗雷德
年龄= 92
任何没有定义
选项​​= Y
anotheroption没有定义
otherparameters = goosefeathers

公约是使用 / 作为开关指示灯 - 领先的 - 是合法的文件名。实现依赖于程序员的奇想。上述结构将允许任何字符 - CMD 的语法规则范围内,自然[即。 $ _#OK,%^!不,!也许()尴尬]

I want to create my own command in following manner which I wish to run from both batch and cmd.exe:

fake-command -name <some value> -age <some Value>

Currently I know to create a command as following:

fake-command  <some value> <another Value>

After that I can collect the input as %1 and %2. But that is no efficient way to do this because what happens if the order in which I am expecting the input gets changed and someone enters age before name.

So I have two questions:

  1. How to create Linux like switches on Windows command line?
  2. Is my approach correct? Is there a better way to do this?

解决方案

@ECHO OFF
SETLOCAL
SET "switches=name age whatever"
SET "noargs=option anotheroption"
SET "swindicators=/ -"
SET "otherparameters="
:: clear switches
FOR %%a IN (%switches% %noargs%) DO SET "%%a="

:swloop
IF "%~1"=="" GOTO process

FOR %%a IN (%swindicators%) DO (
 FOR %%b IN (%noargs%) DO (
  IF /i "%%a%%b"=="%~1" SET "%%b=Y"&shift&GOTO swloop
 )
 FOR %%b IN (%switches%) DO (
  IF /i "%%a%%b"=="%~1" SET "%%b=%~2"&shift&shift&GOTO swloop
 )
)
SET "otherparameters=%otherparameters% %1"
SHIFT
GOTO swloop

:process

FOR %%a IN (%switches% %noargs% otherparameters) DO IF DEFINED %%a (CALL ECHO %%a=%%%%a%%) ELSE (ECHO %%a NOT defined)

GOTO :EOF

Here's a demonstration.

Given you have 3 switches, name age whatever which accept one argument and two true switches, option and anotheroption which take no arguments, then running the above procedure as

q27497516 -age 92 /option goosefeathers /name fred

(q27497516.bat is my batchname here) will yield

name=fred
age=92
whatever NOT defined
option=Y
anotheroption NOT defined
otherparameters= goosefeathers

Convention is to use / as a switch indicator - a leading - is legitimate for a filename. Implementation depends on the programmer's whim. The above structure will allow any character - within cmd's syntax rules, naturally [ie. $_# OK, %!^ No, ! maybe () awkward]

这篇关于添加切换到命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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