commandline icon indicating copy to clipboard operation
commandline copied to clipboard

Keeping everything as is just with HelpText.AdditionalNewLineAfterOption = false

Open eisenfaust12 opened this issue 4 years ago • 2 comments

I would like to keep everything as is just have the additional newlines removed in the help output. I have been trying to achieve this for one hour now, but when I create my own parser with new Parser(with => with.HelpWriter = null) and try to display help myself, it won't work with custom help for custom commands, i.e. myapp help add will no longer print the specific help for the add command.

So how can I just get rid of those additional newlines in the help screens and keep everything else as it is? Is it possible at all?

eisenfaust12 avatar Mar 24 '21 21:03 eisenfaust12

I have something like this right now

        static void Main(string[] args) {
            var parser = new Parser(with => with.HelpWriter = null);
            var result = parser.ParseArguments<OptionsInit, OptionsAdd>(args);
            result.MapResult(
                (OptionsInit o) => 1,
                (OptionsAdd o) => 1,
                errs => DisplayHelp(result, errs));

        }

        static int DisplayHelp<T>(ParserResult<T> result, IEnumerable<Error> errs) {
            var helpText = errs.IsVersion() ? HelpText.AutoBuild(result) :
            HelpText.AutoBuild(result, h => {
                h.Copyright = string.Empty;
                h.AdditionalNewLineAfterOption = false;
                return HelpText.DefaultParsingErrorsHandler(result, h);
            }, e => e, true);
            Console.WriteLine(helpText);
            return 1;
        }
    }

eisenfaust12 avatar Mar 24 '21 21:03 eisenfaust12

I haven't tested myself, but in case you haven't tried already i suggest trying this HelpText.AutoBuild overload instead:

HelpText.AutoBuild<T>(ParserResult<T> parserResult, Func<HelpText, HelpText> onError, int maxDisplayWidth = DefaultMaximumLength)

Looking at its Intellisense/source code documentation, it might perhaps turn out to just do what you want

Creates a custom instance of the CommandLine.Text.HelpText class, automatically handling verbs or options scenario.

https://github.com/commandlineparser/commandline/blob/d443a51aeb3a418425e970542b3b96e9da5f62e2/src/CommandLine/Text/HelpText.cs#L407-L419

elgonzo avatar Mar 25 '21 11:03 elgonzo