fclp / fluent-command-line-parser

A simple, strongly typed .NET C# command line parser library using a fluent easy to use interface
Other
530 stars 85 forks source link

Getting parsing exception #113

Closed astrohart closed 3 years ago

astrohart commented 3 years ago

Hi,

My project has the FluentCommandLineParser NuGet package installed. It's a .NET Framework 4.8 Console Application running on Windows 10 (latest build) with VS 2019 Enterprise.

Here's my Program.cs:

using Fclp;
using System;
using System.Linq;

namespace MyConsoleApp
{
    public static class Program
    {
        public const string ROOT_DIR =
            @"C:\Users";

        public static void Main(string[] args)
        {
            try
            {
                var cmdInfo = CommandLineInfo.ParseCommandLine(args);
                if (cmdInfo == null)
                {
                    Console.WriteLine("ERROR: Failed to parse command line.");
                    Console.ReadKey();

                    Environment.Exit(-1);
                }

                Console.WriteLine($"Root directory = '{cmdInfo.RootDirectory}'");
                Console.WriteLine($"Old PostSharp Version = '{cmdInfo.OldPostSharpVersion}'");
                Console.WriteLine($"New PostSharp Version = '{cmdInfo.NewPostSharpVersion}'");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            Console.ReadKey();
            Environment.Exit(0);
        }
    }

    public class CommandLineInfo
    {
        /// <summary>
        /// Gets or sets a string that contains the new PostSharp version
        /// (number only).
        /// </summary>
        public string NewPostSharpVersion { get; set; } = "6.9.4";

        /// <summary>
        /// Gets or sets a string that contains the new PostSharp version
        /// (number only).
        /// </summary>
        public string OldPostSharpVersion { get; set; } = "6.9.3";

        /// <summary>
        /// Gets or sets a string that contains the starting folder.
        /// </summary>
        public string RootDirectory { get; set; } = Program.ROOT_DIR;

        public static CommandLineInfo ParseCommandLine(string[] args)
        {
            var result = new CommandLineInfo();
            if (args == null || !args.Any())
                return result;

            var p = new FluentCommandLineParser<CommandLineInfo>();

            p.Setup(arg => arg.RootDirectory)
             .As("root")
             .SetDefault(Program.ROOT_DIR);

            p.Setup(arg => arg.NewPostSharpVersion)
             .As("npsv")
             .SetDefault("6.9.4")
             .Required();

            p.Setup(arg => arg.OldPostSharpVersion)
             .As("opsv")
             .SetDefault("6.9.3")
             .Required();

            var parsingResult = p.Parse(args);
            if (parsingResult.HasErrors)
                throw new InvalidOperationException(parsingResult.ErrorText);

            return p.Object;
        }
    }
}

When I click the Project menu and then click Properties, click the Debug tab, and then enter

-root="C:\temp" -npsv="6.9.5" -opsv="6.9.6"

for the command-line arguments to pass to the executable when I hit F5, which I think are the correct format for the arguments, then I get:

System.InvalidOperationException: Option ':npsv' parse error. option is required but was not specified.
Option ':opsv' parse error. option is required but was not specified.

   at MyConsoleApp.CommandLineInfo.ParseCommandLine(String[] args) in C:\Users\Administrator\source\junk\MyConsoleApp\MyConsoleApp\Program.cs:line 82
   at MyConsoleApp.Program.Main(String[] args) in C:\Users\Administrator\source\junk\MyConsoleApp\MyConsoleApp\Program.cs:line 16

I am not sure why I am receiving these errors. I followed along with your documentation and I think I am coding it correctly. Please assist with what I am doing wrong. I think I have written the code correctly. I also think I have written the sample command-line arguments correctly as well.

Thank you in advance.

EricZimmerman commented 3 years ago

long commands are --, not -

try using -- for each command

astrohart commented 3 years ago

Yes, --root="C:\temp" --npsv="6.9.5" --opsv="6.9.6" worked.

Suggestion Make the GitHub Pages documentation more descriptive of this requirement. I am a senior technologist with deep experience in programming, but if I could not understand that long options are always required to begin with -- then perhaps it may be unclear to others.

Suggestion Make the error message during the exception more descriptive of the need to use -- for long parameter names.

EricZimmerman commented 3 years ago

i dont use .Required, but rather check for mandatory options being set. this way you can show the entire help screen. i also add a footer showing example commands with my tools that use this library.

something to consider

https://github.com/EricZimmerman/evtx/blob/master/EvtxECmd/Program.cs#L192

astrohart commented 3 years ago

Thanks for the tip! Really appreciate it!