• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Gnu argp or getopt like arguments processor for Powershell?

D

Duncan Smith

#1
Is there anything like the Gnu 'argp' or 'getopt' parsers from glibc
for processing command line arguments to a PS function? In particular
the self documenting --usage and --help screens would be a real help
and I just wanted to check that there wasn't already something out
there before looking into it.

Many thanks,

Duncan
 

My Computer

R

RickB

#2
How do you use a parser on a directory object?
Arguments are not necessarily text and in fact typically are not text.
To be of any use you would also need what would be analogous to being
able to see the names of the variables used by a caller when calling
your routine.

The command parser is available if you have text that needs parsing.

[Management.Automation.PSParser]::tokenize

static System.Collections.ObjectModel.Collection Tokenize(String
script, Collection`1& errors)
static System.Collections.ObjectModel.Collection Tokenize(Object[]
script, Collection`1& errors)

Duncan Smith wrote:

> Is there anything like the Gnu 'argp' or 'getopt' parsers from glibc
> for processing command line arguments to a PS function? In particular
> the self documenting --usage and --help screens would be a real help
> and I just wanted to check that there wasn't already something out
> there before looking into it.
>
> Many thanks,
>
> Duncan
 

My Computer

J

Jason Archer

#3
I Duncan. Are you aware that PowerShell can already do this for you?

Check this out:

function test ($three) {
write-host "The argument named 'three' was set to '$three'"
write-host "The arguments that did not match my pre-specified list are:
$args"
}

test -one "1" -two "2" -three "3" -four "four"



Arguments can be positional, or named. If you type the argument, then you
will be assured that you will get the write type of object (if a conversion
is not possible, the user will get an error).

In PowerShell v2, even more options will be available to scripts. Like
required arguments, or allowed values.


"Duncan Smith" wrote:

> Is there anything like the Gnu 'argp' or 'getopt' parsers from glibc
> for processing command line arguments to a PS function? In particular
> the self documenting --usage and --help screens would be a real help
> and I just wanted to check that there wasn't already something out
> there before looking into it.
>
> Many thanks,
>
> Duncan
>
 

My Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)