Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

built-in "-help" usage info #1

Closed
doggy8088 opened this issue Oct 31, 2020 · 3 comments
Closed

built-in "-help" usage info #1

doggy8088 opened this issue Oct 31, 2020 · 3 comments
Assignees
Labels
question Further information is requested

Comments

@doggy8088
Copy link

Do you have built-in usage info when program use -help or --help?

@doggy8088 doggy8088 changed the title Do you have built-in "-help" usage info? built-in "-help" usage info Oct 31, 2020
@mikeruhl
Copy link
Owner

Yes, either will work. Given this example:

static void Main(string[] args)
{
    var f = Flag.String("path", string.Empty, "path to file");
    var m = Flag.String("message", "hello world", "message to place in file");
    Flag.Parse();
    Console.WriteLine($"Path supplied: {f}");
    Console.WriteLine($"message: {m}");
}

if I run .\TestApp.exe -help
I will get:

Usage of C:\Users\miker\source\temp\TestApp\TestApp\bin\Debug\netcoreapp3.1\TestApp.dll:
  -message string
        message to place in file (default "hello world")
  -path string
        path to file

It's important to note that this default behavior ignores that you didn't include arguments. If you were to have different error handling like this example:

static void Main(string[] args)
{
    var flagSet = new FlagSet("main", ErrorHandling.PanicOnError);
    var f = flagSet.String("path", string.Empty, "path to file");
    var m = flagSet.String("message", "hello world", "message to place in file");
    flagSet.Parse(args);
    Console.WriteLine($"Path supplied: {f}");
    Console.WriteLine($"message: {m}");
}

It will still print the help message but also throw an exception, which will result in a non-zero exit code .\TestApp.exe -help:

Usage of main:
  -message string
        message to place in file (default "hello world")
  -path string
        path to file
Unhandled exception. System.InvalidOperationException: flag: help requested
   at GoFlag.FlagSet.Parse(String[] arguments)
   at TestApp.Program.Main(String[] args) in C:\Users\miker\source\temp\TestApp\TestApp\Program.cs:line 15

Any other questions? Thanks, I will add this to the documentation.

@doggy8088
Copy link
Author

I think you can wrap a try/catch in the Flag.Parse(); for displaying usage information purpose. You can simply just put sample code in your docs.

@mikeruhl mikeruhl added the question Further information is requested label Nov 22, 2020
@mikeruhl mikeruhl self-assigned this Nov 22, 2020
@mikeruhl
Copy link
Owner

Added some additional documentation around error handling here: 7ca5f38

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants