Issue by ptr727
_Tuesday May 23, 2017 at 17:02 GMT_
_Originally opened as https://github.com/gsscoder/commandline/issues/445_
How do I print the help output I've evaluated the logical options and something is wrong?
// Parse the commandline arguments
// https://github.com/gsscoder/commandline
Parser parser = new Parser(with =>
{
with.CaseSensitive = false;
with.HelpWriter = Console.Error;
});
ParserResult<Options> result = parser.ParseArguments<Options>(args);
if (result.Tag == ParserResultType.NotParsed)
{
return -1;
}
Options options = ((Parsed<Options>)result).Value;
// Process or monitor must be specified
if (options.ProcessFolders == false && options.MonitorFolders == false)
{
// TODO : Call help output directly
return -1;
}
Comment by NVanderEnde
_Wednesday May 24, 2017 at 15:53 GMT_
Are you looking for HelpText.AutoBuild(args)?
Comment by NVanderEnde
_Friday May 26, 2017 at 16:20 GMT_
Like so:
private static string HelpOutput(Parameters args)
{
var result = new StringBuilder();
result.AppendLine("Hello, and welcome to the console application.");
result.AppendLine("This application takes in a data file and attempts to import that data into our systems.");
result.AppendLine("Valid options are:");
result.AppendLine(HelpText.AutoBuild(args));
result.AppendLine("Press any key to exit");
return result.ToString();
}
where Parameters is some class that's been annotated with the Option attributes, etc.
Comment by ptr727
_Friday May 26, 2017 at 20:14 GMT_
I get a runtime exception:
// Parse the commandline arguments
// https://github.com/gsscoder/commandline
Parser parser = new Parser(with =>
{
with.CaseSensitive = false;
with.HelpWriter = Console.Error;
});
ParserResult<Options> result = parser.ParseArguments<Options>(args);
if (result.Tag == ParserResultType.NotParsed)
{
Tools.WriteLineError("Failed to parse commandline.");
return -1;
}
Options options = (((Parsed<Options>)result).Value);
// Process or monitor must be specified
if (options.ProcessFolders == false && options.MonitorFolders == false)
{
Tools.WriteLineError(CommandLine.Text.HelpText.AutoBuild(result));
Tools.WriteLineError("Failed to parse commandline, missing action.");
return -1;
}
Fails at runtime.
System.ArgumentException occurred
HResult=0x80070057
Message=Excepting NotParsed<T> type.
Source=CommandLine
StackTrace:
at CommandLine.Text.HelpText.AutoBuild[T](ParserResult`1 parserResult, Int32 maxDisplayWidth)
at Plex.Program.Main(String[] args) in C:\Users\piete\OneDrive\Projects\Plex\Plex\Program.cs:line 44
Comment by cbertolasio
_Wednesday Jun 14, 2017 at 18:10 GMT_
I also get the same error at runtime using my own sample project. Probably a different topic... but it is unclear what the best practices are to get the helptext to be output when the args follow the "help verb" or "help" construct using the v2.x codebase - documentation is dated and so is the sample.
```c#
static int Main(string[] args)
{
var results = Parser.Default.ParseArguments
var exitCode = 0;
if (args[0].Equals("help", StringComparison.InvariantCultureIgnoreCase))
{
var text = HelpText.AutoBuild(results);
Console.WriteLine(text.ToString());
return 0;
}
else
{
exitCode = results.MapResult(
(SetupDirectionCommandOptions opts) => RunSetupDirection(opts),
errs => (int)1);
}
return exitCode;
}
this is what my verb looks like...
```c#
[Verb(Verbs.SetupDirectionKey, HelpText = "Setup a direction command")]
public class SetupDirectionCommandOptions : SubOptionsBase
{
[Option('r', "rotation-type", Default = PivotRotation.None, HelpText = "Set the pivot direction")]
public PivotRotation Direction { get; set; }
[Option('v', "velocitymode-type", Default = VelocityMode.AutoVri, HelpText = "Set the velocity mode")]
public VelocityMode VelocityModeType { get; set; }
[Option("percent", Default = 100, HelpText = "Set the speed percentage")]
public int Percent { get; set; }
[Option('f', "flowrate", Default = 6, HelpText = "Set the flow rate")]
public float FlowRate { get; set; }
[Option("ispumpon", Default = true, HelpText = "Set the is pump on flag")]
public bool IsPumpOn { get; set; }
}
Comment by veleek
_Tuesday Jul 25, 2017 at 23:23 GMT_
I believe that this is definitely a bug in the code. If you use the overload of AutoBuild(...) that takes onError and onExample functions then it will write successfully.
It actually has an explicit block in the code which checks whether or not the ParserResult<T> being passed in is a NotParsed result in which case it gracefully attempts to write the error output.
if (onError != null && parserResult.Tag == ParserResultType.NotParsed)
{
errors = ((NotParsed<T>)parserResult).Errors;
if (errors.OnlyMeaningfulOnes().Any())
auto = onError(auto);
}
So, as a workaround in the meantime you can just use
Console.WriteLine(HelpText.AutoBuild(result, null, null));
The above workaround does not work for me (throws a NullReferenceException, using CommandLineParser 2.6.0), however only a simple change was needed to make it work:
Console.WriteLine(HelpText.AutoBuild(result, _ => _, _ => _));
Most helpful comment
_Tuesday Jul 25, 2017 at 23:23 GMT_
I believe that this is definitely a bug in the code. If you use the overload of
AutoBuild(...)that takesonErrorandonExamplefunctions then it will write successfully.It actually has an explicit block in the code which checks whether or not the
ParserResult<T>being passed in is a NotParsed result in which case it gracefully attempts to write the error output.So, as a workaround in the meantime you can just use