Arvo Bowen
Arvo Bowen

Reputation: 4929

CommandLineParser - Show --help results if no switch is given

Using the CommandLineParser NuGet, when I run my application with NO arguments, is it possible to forcefully show the --help results output just as if I was to run my application like...

myapplication.exe --help

Currently, when I run my application it does not show the help output when I specify no options. It just ends the application. I have many different options/flag/arguments that can be used. None of them should be forced on their own but I need at least one to be used or show help.

My current implementation...

public class Options
{
    [Option(
        'v',
        Required = false,
        HelpText = "Shows all debug information when processing."
    )]
    public bool Verbose { get; set; }

    [Option(
        Required = false,
        HelpText = "Runs Test One."
    )]
    public bool TestOne { get; set; }
}

static void Main(string[] args)
{
    try
    {
        var parserResults = Parser.Default.ParseArguments<Options>(args);

        parserResults
            .WithParsed<Options>(options => Run(options));
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }

    Console.WriteLine("Main thread closing.");
}

static void Run(Options options)
{
    // Verbose mode
    if (options.Verbose)
    {
        m_Verbose = true;
        Console.WriteLine("Verbose mode on.");
    }
    
    // Test
    if (options.TestOne)
    {
        //do test
    }
}

Upvotes: 2

Views: 4000

Answers (2)

Jaco B
Jaco B

Reputation: 1043

Yes, just add the following as the first 2 lines of your Main method:

if (args.Length == 0)
  args = new [] { "--help" };

Upvotes: 5

InteXX
InteXX

Reputation: 6367

I was able to get this working using these two tips:

Here's some example code:

using CommandLine;
using CommandLine.Text;

internal static class Main
{
    public static void Main(string[] Arguments)
    {
        HelpText oHelpText;
        ParserResult<Command.Options> oResult;

        oResult = Parser.Default.ParseArguments<Command.Options>(Arguments);
        oResult.Success(Options => Run(Options));

        oHelpText = HelpText.AutoBuild(oResult, x => x, x => x);

        Console.WriteLine(oHelpText);
        Console.ReadLine();
    }

    private static void Run(Command.Options Options)
    {
        switch (Options.Type)
        {
            case Command.Types.Folder:
                {
                    break;
                }

            case Command.Types.File:
                {
                    break;
                }
        }
    }
}

internal static class Extensions
{
    public static ParserResult<Command.Options> Success(this ParserResult<Command.Options> Instance, Action<Command.Options> Action)
    {
        return Instance.WithParsed(Action);
    }

    public static ParserResult<Command.Options> Failure(this ParserResult<Command.Options> Instance, Action<Command.Options> Action)
    {
        return Instance.WithNotParsed(Action);
    }
}

namespace Command
{
    internal class Options
    {
        [Option("s", "source", HelpText = "The source folder that contains the designated files/subfolders")]
        public string Source { get; set; }

        [Option("t", "target", HelpText = "The target folder to which to move the files/subfolders")]
        public string Target { get; set; }

        [Option("y", "type", HelpText = "The source type [File | Folder]")]
        public Enums.Types Type { get; set; }

        [Option("c", "chunksize", HelpText = "The size of each chunk to move")]
        public int ChunkSize { get; set; }
    }

    internal static class Enums
    {
        public enum Types
        {
            Folder,
            File
        }
    }
}

Upvotes: 1

Related Questions