【问题标题】:CommandLineParser - Show --help results if no switch is givenCommandLineParser - 如果没有给出开关,则显示 --help 结果
【发布时间】:2021-06-04 05:51:29
【问题描述】:

使用CommandLineParser NuGet,当我在没有参数的情况下运行我的应用程序时,是否可以强制显示 --help 结果输出,就像我要运行我的应用程序一样......

myapplication.exe --help

目前,当我运行我的应用程序时,如果我没有指定任何选项,它不会显示帮助输出。它只是结束应用程序。我有许多不同的选项/标志/参数可以使用。他们都不应该被强迫自己,但我至少需要一个来使用或提供帮助。

我目前的实现...

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
    }
}

【问题讨论】:

  • @Sinatr 我想你可能误解了我的问题。因此,我对其进行了详细说明,希望它不值得被否决……我遇到的问题是,当我在没有参数的情况下运行我的应用程序时,我只能从我的示例中得到测试Main thread closing.。我根本没有收到任何帮助文本,我希望它给我帮助文本。
  • 你有异常吗?
  • 什么都没有。让我觉得这是预期的用途。
  • 除了我的回答之外,您还可以尝试Options 属性上的Group 属性:github.com/commandlineparser/commandline/wiki/Option-Attribute

标签: c# command-line-arguments command-line-parser


【解决方案1】:

我能够使用以下两个技巧来完成这项工作:

下面是一些示例代码:

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
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 2015-12-15
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 2012-04-08
    • 1970-01-01
    相关资源
    最近更新 更多