0

I have a function

getinfo([string]$subcommand, [string]$argument)

What I would like to do is allow the argument to be of value '-h' or '-help' such that

getinfo hosts -h

would give a help text for the subcommand 'hosts'. Unfortunately, when i specify -h I think it doesn't assign the value of "-h" to $argument and instead creates a new argument.

I was thinking of using a switch parameter but I'm not sure if I can use that in a function in conjunction with $argument above.

Is this possible?

1
  • What do you mean "C# switch parameters"? Commented Nov 25, 2014 at 23:08

2 Answers 2

5

Stepping back a bit, you are trying to force a command paradigm on PowerShell that is not what I'd call idiomatic PowerShell. Why not create several functions each with their own help e.g. Get-HostInfo, Get-FooInfo, etc. Then you can use PowerShell's built-in documentation support e.g.:

<#
.SYNOPSIS
   Short description
.DESCRIPTION
   Long description
.PARAMETER Host
    Name of the host.
.EXAMPLE
   Example of how to use this cmdlet
.EXAMPLE
   Another example of how to use this cmdlet
#>
function Get-HostInfo($host) { ... }

This supports help in all the ways PowerShell users expect:

man Get-HostInfo
man Get-HostInfo -Example
man Get-HostInfo -Parameter Host
Get-HostInfo -?
Sign up to request clarification or add additional context in comments.

1 Comment

The built-in documentation is the way to go for defining help information. You want to be able to type Get-Help and the command should produce the needed help text. Writing your own code to do this breaks that idea. Let PowerShell do what it is good at.
0

I'd do it like this:

getinfo([string]$subcommand, [switch][alias("h")]$Help) {
  switch ($subcommand) {
    'hosts' {
       if ($Help) { 'Usage: getinfo hosts ...'; return }
       ...
    }
    ...
  }
}

All arguments not matching a parameter are automatically stored in the variable $args, so you don't really need a parameter $argument. It has the advantage (or disadvantage, depending on your point of view), that it accepts even arguments that PowerShell would interpret as parameters. Example:

PS C:\> function getinfo($subcommand, [switch]$Help, $argument) {
>>   if ($Help) { 'help' }
>>   $argument
>> }
>>
PS C:\> getinfo 'a' 'b'
b
PS C:\> getinfo 'a' -x
PS C:\> getinfo 'a' 'b' -h
help
b
PS C:\> getinfo 'a' -x -h
help
PS C:\> function getinfo($subcommand, [switch]$Help) {
>>   if ($Help) { 'help' }
>>   $args[0]
>> }
>>
PS C:\> getinfo 'a' 'b'
b
PS C:\> getinfo 'a' -x
-x
PS C:\> getinfo 'a' 'b' -h
help
b
PS C:\> getinfo 'a' -x -h
help
-x

1 Comment

That worked great. Thanks so much. I've got info([string]$subcommand, [string]$argument, [string]$filter, [switch][alias("h")]$help). Worked perfectly because as long as -h is set, i'm in business. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.