【问题标题】:PowerShell - Processing Command - Line Parameters In Correct OrderPowerShell - 以正确的顺序处理命令行参数
【发布时间】:2020-08-20 08:02:23
【问题描述】:

我有一个关于命令行参数处理的问题 在 PowerShell 中:

我开发了一个结构来接收命令行参数,实际上允许3个参数:“-Help”、“-Step”和“-Config”。

问题与处理参数的方式有关。在我的概念中,脚本总是首先检查是否存在“-Config”参数。之后它应该加载基本设置并最后处理“-Step”参数以确保加载配置。

我也期待,如果用户输入“-Help”参数,不管指定哪个参数,脚本只会输出帮助对话框。

我确实试图削减大部分代码,但我想,所有仍然存在的部分都是必要的。

如果你在命令行上执行这样的脚本:

.\AI_Installer.ps1 -Config C:\Scripts\DCT01.cfg -Step Configure_Server -Help

输出将是:

[ 5 ] Configuring Server Settings ...
[ 1 ] User - Defined Config Found ! [ C:\Scripts\DCT01.cfg ] 0
[ 2 ] The Directory For The User - Defined Config Is Valid ! [ C:\Scripts\DCT01.cfg ]
[ 3 ] User - Defined Config Is Vaild ! [ DCT01.cfg ]
This Is The Help Sector !
[ 4 ] Loading Basic Settings ...

为了便于理解,我已经对步骤进行了编号, 我的意思;)

最好,如果我可以在“Initialize_Parameters”中手动定义,首先处理哪个参数,第二个,最后一个等等。

我希望你能理解我正在尝试做的事情,并且你能够为我提供解决方案。

非常感谢。

最好的问候NumeroUnoDE

【问题讨论】:

    标签: powershell command-line


    【解决方案1】:

    可以定义parameter set,以便允许一组参数。这在 Powershell cmdlet 中非常典型。例如,Get-Process 允许进程名称或 ID,但不能同时使用两者。

    一个简单的例子要么需要帮助,要么需要两个非帮助参数,就像这样,

    function Test-ParameterSets {
        [CmdletBinding()]
        param(
            [Parameter(Mandatory,ParameterSetName = 'Config')]
            [string]$config,
            [Parameter(Mandatory,ParameterSetName = 'Config')]
            [string]$step,
            [Parameter(Mandatory, ParameterSetName = 'Help')]
            [switch]$help
        )
        if($config) { "Config section $config $step" }
        if($help) { "Help section $help" }
    }
    
    # Call with two valid parameters
    Test-ParameterSets -config "config.cfg" -step "step 001"
    Config section config.cfg step 001
    
    # Call with one valid parameters
    Test-ParameterSets -help                                                                               
    Help section True
    
    # Call with invalid parameters (no help and others)
    Test-ParameterSets -config "config.cfg" -step "step 001" -help                                         Test-ParameterSets : Parameter set cannot be resolved using the specified named parameters.
    
    # Call with missing parameter
    Test-ParameterSets -config "config.cfg"
    cmdlet Test-ParameterSets at command pipeline position 1
    Supply values for the following parameters:
    step:       
    

    【讨论】:

      【解决方案2】:
      $Global_Switches = $args $Global_Switches_Store = @{ } $Global_Switches_Values_Store = @( ) $AI_Identifier = $myInvocation.MyCommand.Definition $AI_Server_Config = @{ } $AI_Debug_Mode = "是" 函数初始化 { 如果(!($Global_Switches)) { $Global_Switches_Store['-Step'] = @("Configure_Server") Load_Basic_Settings 选择器 } 如果($Global_Switches) { 初始化参数 选择器 Load_Basic_Settings } } 函数 Initialize_Parameters { $Global_Switches_Entries = $Global_Switches.Count $Global_Switches_Parameter 对于($Global_Switches_Counter = 0;$Global_Switches_Counter -lt $Global_Switches_Entries;$Global_Switches_Counter++) { 如果($Global_Switches[$Global_Switches_Counter].Contains(“-”)) { if(!($Global_Switches_Store.Contains($Global_Switches[$Global_Switches_Counter]))) { $Global_Switches_Values = @( ) $Global_Switches_Parameter = $Global_Switches[ $Global_Switches_Counter ] } 别的 { 写入主机“参数'$($Global_Switches[$Global_Switches_Counter])'已经存在!” } } 别的 { $Global_Switches_Values += $Global_Switches[ $Global_Switches_Counter ] } if( $Global_Switches_Parameter ) { $Global_Switches_Store[ $Global_Switches_Parameter ] = $Global_Switches_Values } } } 功能选择器{ $Available_Switches = @( "-Config" , "-Help" , "-Step" ) foreach($Global_Switches_Store.Keys 中的 $Switches_Parameter) { if($Available_Switches.Contains($Switches_Parameter)) { if( $Switches_Parameter -eq "-Config" ) { $Switches_Values_Entries = $Global_Switches_Store[ $Switches_Parameter ].Count if( $Switches_Values_Entries -eq 0 ) { Write-Host "需要参数 '$Switches_Parameter' 的值!" ;出口 } if( $Switches_Values_Entries -gt 1 ) { Write-Host "参数 '$Switches_Parameter' 只允许一个值!" ;出口 } $IMWS_Config_User_Defined = $Global_Switches_Store[ $Switches_Parameter ] 写入主机“[1] 找到用户定义的配置![$IMWS_Config_User_Defined]”0 如果(测试路径 $IMWS_Config_User_Defined ) { 写入主机“[2] 用户定义配置的目录有效![$IMWS_Config_User_Defined]” $Config_File_Handler = 拆分路径 $IMWS_Config_User_Defined -leaf $Config_File_EXTN = [IO.Path]::GetExtension( $Config_File_Handler ) if( $Config_File_EXTN -ne ".cfg" ) { 写入主机“[3] 用户定义的配置无效![$Config_File_Handler]” 出口 } 别的 { 写入主机“[3] 用户定义的配置有效![$Config_File_Handler]” } } 别的 { 写入主机“[2] 未找到用户定义的配置![$IMWS_Config_User_Defined]” 出口 } } if( $Switches_Parameter -eq "-Help" ) { 写主机“这是帮助部门!” } if( $Switches_Parameter -eq "-Step" ) { $Switches_Values_Entries = $Global_Switches_Store[ $Switches_Parameter ].Count 如果( $Switches_Values_Entries -eq 0 ) { 写入主机“需要参数 '$Switches_Parameter' 的值!” # 出口 } 如果( $Switches_Values_Entries -gt 1 ) { 写入主机“参数'$Switches_Parameter'只允许一个值!” # 出口 } $Switches_Parameters = $Global_Switches_Store[ $Switches_Parameter ] 对于($Switches_Values_Counter = 0;$Switches_Values_Counter -lt $Switches_Values_Entries;$Switches_Values_Counter++) { $Switches_Values = $Switches_Parameters[ $Switches_Values_Counter ] if( $Switches_Values -eq "Configure_Server" ) { Configure_Server } } } } 别的 { 写主机“错误的参数'$Switches_Parameter'!” } } }

      【讨论】:

        猜你喜欢
        • 2011-01-10
        • 2015-05-01
        • 1970-01-01
        • 2023-04-06
        • 1970-01-01
        • 2013-05-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多