PowerShell

更新于 2024-05-15

解析参数

function ParseArguments($arguments){
    $options = @{};
    $switches = @();
    $raws = @();
    for($i=0; $i -lt $arguments.Length; $i++){
        $arg = $arguments[$i]
        if($arg -like "--*"){
            if($i -eq ($arguments.Length - 1)) {
                $switches += $arg;
                continue;
            }
            $nextValue = $arguments[$i + 1];
            if($nextValue -like "--*"){
                $switches += $arg;
                continue;
            }
            $options[$arg] = $nextValue
            $i++;
            continue;
        }
        if($arg -like "-*"){
            $switches += $arg;
            continue;
        }
        $raws += $arg;
    }
    return @{
        Options=$options
        Switches=$switches
        Raws=$raws
    };
}
class Arguments {
    hidden [hashtable]$options = @{};
    hidden [array]$switches = @();
    hidden [array]$raws = @();

    Arguments ([array]$arguments) {
        for($i=0; $i -lt $arguments.Length; $i++){
            $arg = $arguments[$i]
            if($arg -like "--*"){
                $arg = $arg.substring(2)
                if($i -eq ($arguments.Length - 1)) {
                    $this.switches += $arg;
                    continue;
                }
                $nextValue = $arguments[$i + 1];
                if($nextValue -like "--*"){
                    $this.switches += $arg;
                    continue;
                }
                $this.options[$arg] = $nextValue
                $i++;
                continue;
            }
            if($arg -like "-*"){
                $arg = $arg.substring(1)
                $this.switches += $arg;
                continue;
            }
            $this.raws += $arg;
        }
    }

    [string]GetOption([string]$key, [string] $defaultValue){
        return $this.options[$key] ?? $defaultValue;
    }
    [string]GetRaw([int]$index){
        return $this.raws[$index];
    }
    [boolean]HasOption([string]$key){
        return $this.options.Keys -contains $key;
    }
    [boolean]NotHasOption([string]$key){
        return $this.options.Keys -notcontains $key;
    }
    [boolean]HasSwitch([string]$key){
        return $this.switches -contains $key;
    }
    [boolean]HasSwitchCi([string]$key){
        return $this.switches -ccontains $key;
    }
    [boolean]NotHasSwitch([string]$key){
        return $this.switches -notcontains $key;
    }
    [boolean]NotHasSwitchCi([string]$key){
        return $this.switches -cnotcontains $key;
    }
}

输入

$password = Read-Host "Please input password" -MaskInput
$text = Read-Host "Please input username"

数组

$array = @(1, 2, 3)
$text = $array -join ' '
$array -contains 1
$array -notcontains 1
$array = @("lilith", "malier")
$array -ccontains "lilith"
$array -ccontains "Lilith"

字符串

$string = "hello world"
$string.IndexOf(' ')
$string.Split(' ')
$string.ToLower()
$string.ToUpper()
$string.Trim()
$string.TrimStart()
$string.TrimEnd()

日期

$start = (Get-Date);

$end = (Get-Date);
$timeUsed = "{0:0.00}" -f ($end - $start).TotalSeconds