2012-09-11 23 views
16

Ben bu temel adımları argümanları kabul edecek bir powershell (2.0) komut oluşturmak çalışıyorum:Powershell olmayan pozisyonel, isteğe bağlı parametreler

seçenekleri opsiyonel parametrelerin herhangi sayıda
.\{script name} [options] PATH 

- boyunca düşünmek Verbose için '-v' satırları. PATH argümanı, son olarak hangi argümanın geçtiyse gerçekleşir ve zorunludur. Senaryoyu hiçbir seçenek ve sadece bir argüman olmadan arayabilir ve bu argümanın Yol olduğu varsayılır. Sadece isteğe bağlı parametreleri içeren, ancak aynı zamanda konumsal olmayan bir parametre listesi oluşturmada sorun yaşıyorum. şöyle

#param test script 
Param(
    $firstArg, 
    $secondArg, 
    [switch]$thirdArg, 
    [Parameter(ValueFromRemainingArguments = $true)] 
    $remainingArgs) 

write-host "first arg is $firstArg" 
write-host "second arg is $secondArg" 
write-host "third arg is $thirdArg" 
write-host "remaining: $remainingArgs" 

seslendi::

.\param-test.ps1 firstValue secondValue 

komut çıkışları:

first arg is firstValue 
second arg is secondValue 
third arg is False 
remaining: 

denemekten duyuyorum davranış

Bu hızlı komut ben yaşıyorum sorunu gösterir Oluşturulacak, her iki argümanın da İsteğe bağlı params ve reverseArgs değişkeninde biter.

This question/answer

yardımsever istenilen davranışı elde etmek için bir yol sağladı, ama orada en az bir zorunlu parametredir ve bu geliyorsa eğer sadece iş gibi görünüyor diğer argümanlardan öncesi tüm.

I firstArg zorunlu hale ve 0 olan bir pozisyon belirtilerek bu davranışı göstermek

:

.\param-test.ps1 firstValue secondValue 

çıkış aşağıdaki gibidir: önceki ile aynı girişi ile

#param test script 
Param(
    [Parameter(Mandatory=$true, Position = 0)] 
    $firstArg, 
    $secondArg, 
    [switch]$thirdArg, 
    [Parameter(ValueFromRemainingArguments = $true)] 
    $remainingArgs) 

    write-host "first arg is $firstArg" 
    write-host "second arg is $secondArg" 
    write-host "third arg is $thirdArg" 
    write-host "remaining: $remainingArgs" 

Run

first arg is firstValue 
second arg is 
third arg is False 
remaining: secondValue 

İlk, zorunlu argüman atanır ve kalan her şey soluk düşer h.

soru

şudur: Ben bir params listesi params şekilde tüm ayarlayabilirsiniz nasıl isteğe bağlıdır ve bunların hiçbiri pozisyonel nedir?

cevap

23

Bu nasıl?

function test 
{ 
    param(
     [string] $One, 

     [string] $Two, 

     [Parameter(Mandatory = $true, Position = 0)] 
     [string] $Three 
    ) 

    "One = [$one] Two = [$two] Three = [$three]" 
} 

One ve Two isteğe bağlıdır ve sadece adı ile belirlenebilir. Three zorunludur ve isimsiz olarak sağlanabilir.

Bu çalışma:

test 'foo' 
    One = [] Two = [] Three = [foo] 
test -One 'foo' 'bar' 
    One = [foo] Two = [] Three = [bar] 
test 'foo' -Two 'bar' 
    One = [] Two = [bar] Three = [foo] 

Bu başarısız olur:

test 'foo' 'bar' 
test : A positional parameter cannot be found that accepts argument 'bar'. 
At line:1 char:1 
+ test 'foo' 'bar' 
+ ~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidArgument: (:) [test], ParameterBindingException 
    + FullyQualifiedErrorId : PositionalParameterNotFound,test 

Bu zorunlu arg son sırayı alır, ya da adlandırılmış değil o zorlamaz gelmez.Ancak, , istediğiniz temel kullanım deseni için'a izin verir.

Ayrıca, $Three'da birden fazla değere izin verilmez. İstediğin bu olabilir. Ancak, birden çok adlandırılmamış params'ı $Three'un parçası olarak ele almak istiyorsanız, ValueFromRemainingArguments özniteliğini ekleyin. Bu işi gibi Şimdi

function test 
{ 
    param(
     [string] $One, 

     [string] $Two, 

     [Parameter(Mandatory = $true, Position = 0, ValueFromRemainingArguments = $true)] 
     [string] $Three 
    ) 

    "One = [$one] Two = [$two] Three = [$three]" 
} 

nokta:

test -one 'foo' 'bar' 'baz' 
    One = [foo] Two = [] Three = [bar baz] 

Ya Tam olarak ne gerekli, hatta

test 'foo' -one 'bar' 'baz' 
    One = [bar] Two = [] Three = [foo baz] 
+0

. _last_ argümanı olarak Position = 0 ile zorunlu bir argüman kullanabileceğimi bana göstermedi. – Fopedush

+0

teşekkürler latkin, bunun üzerinde bir saat bile yakmıştım. Başka bir sorum vardı (size kredi ile cevap verdim): http://stackoverflow.com/questions/18861701/how-do-i-force-declared-parameters-to-require-explicit-naming – ekkis

İlgili konular