-
-
Notifications
You must be signed in to change notification settings - Fork 2
parsing powershell options
Since version v0.7.0, getopt.net supports using Powershell conventions for options.
Enabling support for Powershell conventions (while not enabled by default), does not disable support for GNU/POSIX conventions!
Powershell-style command-line options are prefixed with a single dash - for both long and short options.
The support for Powershell conventions can be enhanced by also enabling support for Windows-style options.
Enabling Windows conventions allows your users to separate options from their args by using a colon (:), while also supporting the standard equals (=), and spaces.
private string _filePath = ".file.txt";
// only ShortOpts
// called with myapp -h
// called with myapp -v
// called with myapp -c/path/to/file
// called with myapp -c /path/to/file
// called with myapp -config /path/to/file
// called with myapp -config:/path/to/file # Windows conventions
public static void Main(string[] args) {
var getopt = new GetOpt {
ShortOpts = "hvc:",
Options = new[] {
new Option("config", ArgumentType.Required, 'c') // just an example here
}
AppArgs = args, // pass the args parameter
AllowPowershellConventions = true
};
var optChar = 0;
while ((optChar = getopt.GetNextOpt(out var optArg)) != -1) {
switch (optChar) {
case 'h':
PrintHelp();
return;
case 'v':
PrintVersion();
return;
case 'c':
_filePath = optArg;
break;
}
}
}
void PrintHelp() {
Console.WriteLine(
"""
myapp v1.0.0
myapp shows off getopt.net :)
Usage:
myapp
myapp [options]
Arguments:
-h Displays this menu and exits
-v Displays the version info and exits
-c [file] Overrides the file
"""
);
}
printVersion() => Console.WriteLine("myapp v1.0.0");private string _filePath = ".file.txt";
// only Options
// also works with short options!
// called with myapp /help
// called with myapp /h
// called with myapp /version
// called with myapp /v
// called with myapp /config=/path/to/file
// called with myapp /config /path/to/file
// called with myapp /c/path/to/file
// called with myapp /c /path/to/file
// called with myapp /help
// called with myapp /h
// called with myapp /version
// called with myapp /v
// called with myapp /config=/path/to/file
// called with myapp /config /path/to/file
// called with myapp /c/path/to/file
// called with myapp /c /path/to/file
public static void Main(string[] args) {
var getopt = new GetOpt {
Options = new[] {
new Option("help", ArgumentType.None, 'h'),
new Option("version", ArgumentType.None, 'v'),
new Option("config", ArgumentType.Required, 'c')
}
AppArgs = args, // pass the args parameter
AllowWindowsConventions = true
};
var optChar = 0;
while ((optChar = getopt.GetNextOpt(out var optArg)) != -1) {
switch (optChar) {
case 'h':
PrintHelp();
return;
case 'v':
PrintVersion();
return;
case 'c':
_filePath = optArg;
break;
}
}
}
void PrintHelp() {
Console.WriteLine(
"""
myapp v1.0.0
myapp shows off getopt.net :)
Usage:
myapp
myapp [options]
Arguments:
--help, -h Displays this menu and exits
--version, -v Displays the version info and exits
--config, -c [file] Overrides the file
"""
);
}
printVersion() => Console.WriteLine("myapp v1.0.0");Written with ❤️ by Simon Cahill.
Have you contributed to this wiki? Don't forget to add your name(s) up above!
Can't find a solution to your problem? Create an Issue Want to share your cool developments/improvements with the rest of us? Create a Pull Request
If no icons are shown, please reload the page.
All icons procured from https://www.iconfinder.com/iconsets/essentials-pack