如何强制声明的参数需要显式命名? [英] How do I force declared parameters to require explicit naming?

查看:50
本文介绍了如何强制声明的参数需要显式命名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难考虑清楚.如果我希望所有声明的参数在设置时都需要显式命名,并且我想从 $args 变量中选取任何未命名的内容,我该怎么做?

I'm having a hard time thinking this through. if I wanted all declared parameters to require explicit naming when they're set and I wanted to pick up anything unnamed from the $args variable, how would I do it?

如果我的脚本声明了以下参数:

If my script declared the following parameters:

param($installdir, $compilemode, $whatever) 

然后传递文件列表(在这种情况下我不想指定安装目录、编译模式等),前三个参数会占用我的参数.所以,我想通过:

then passing a list of files (in which case I don't want to specify the installation directory, a compile mode, etc.) the first three parameters would gobble my arguments. So, I would like to pass:

c:\> MyScript file-1.cs file-2.cs file-3.cs file-4.cs 

并让所有 4 个字符串都出现 $args,或者调用:

and get all 4 strings appear $args, or alternatively, call:

c:\> MyScript -CompileMode simple file-1.cs -InstallDir c:\temp file-2.cs

并使用包含 2 个文件的 $args 获取 $compilemode 和 $installdir 的值...我该怎么做?

and get values for $compilemode and $installdir with $args containing 2 files... how can I do that?

推荐答案

好的,想通了.像这样声明:

ok, figured it out. declare like this:

param(
   $installdir, 
   $compilemode, 
   $whatever, 
   [Parameter(Position=0, ValueFromRemainingArguments=$true)] $args
   )

感谢@latkin,他已经在以下位置回答了这个问题:Powershell 非位置可选参数

thanks to @latkin, who had already answered this at: Powershell non-positional, optional params

这篇关于如何强制声明的参数需要显式命名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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