Commandline: Need a simpler example

Created on 26 Apr 2018  路  9Comments  路  Source: commandlineparser/commandline

the example demo is wayyyy too complicated. 90% of people don't need that level of command line complexity. provide several examples each of varying simplicity.

Most helpful comment

I like this approach. See if you feel that this is easier to understand:

        // Commandline options
        internal class CmdOptions
        {
            [Option('f', "file", Required = true, HelpText = "Database file to open")]
            public string DBFile { get; set; }

            [Option("driveletter", Default = @"X:\", HelpText = "Driveletter to use")]
            public string DriveLetter { get; set; }
        }

        //Entrypoint
        static void Main(string[] args)
        {
            var cmdOptions = Parser.Default.ParseArguments<CmdOptions>(args);
            cmdOptions.WithParsed(
                options => {
                    Main(options);
                });

        }

        //Overloaded main, called with CmdOptions from main(string[])
        static void Main(CmdOptions options)
        {
            Console.WriteLine($"Database file used {options.DBFile}");
            string dbFile = options.DBFile;

            Console.WriteLine($"Mounting to drive {options.DriveLetter}");
            string driveLetter = options.DriveLetter;
         }

All 9 comments

I totally concur. Well, such mind-twisting example is just defeating the whole point of using this library in the first place. It is rather easier to deal with low-level parsing of command line myself.

I am very confused with new version. I need to see a simple argument parsing demo

@g2david @sampomike
https://github.com/commandlineparser/commandline/pull/274
this might help. i have not verified it because i stopped using this library due to tons of issues when using it within mono

I like this approach. See if you feel that this is easier to understand:

        // Commandline options
        internal class CmdOptions
        {
            [Option('f', "file", Required = true, HelpText = "Database file to open")]
            public string DBFile { get; set; }

            [Option("driveletter", Default = @"X:\", HelpText = "Driveletter to use")]
            public string DriveLetter { get; set; }
        }

        //Entrypoint
        static void Main(string[] args)
        {
            var cmdOptions = Parser.Default.ParseArguments<CmdOptions>(args);
            cmdOptions.WithParsed(
                options => {
                    Main(options);
                });

        }

        //Overloaded main, called with CmdOptions from main(string[])
        static void Main(CmdOptions options)
        {
            Console.WriteLine($"Database file used {options.DBFile}");
            string dbFile = options.DBFile;

            Console.WriteLine($"Mounting to drive {options.DriveLetter}");
            string driveLetter = options.DriveLetter;
         }

If I wanted to write Scala (or any other functional language), I would do so.

@Rainmaker52
Pardon the month+ necro, but I actually just finished struggling through the documentation by rubber-ducking it with someone in a .NET chatroom, and I definitely like the format of your approach. I'd love to help/be kept in the loop about updating the documentation.

@Rainmaker52 I second this. The only thing is that if main returns an int this setup dosen't work. I used this for mine.

CmdOptions opts;
Parser.Default.ParseArguments<Options>(args).WithParsed(options => { opts = options; });

@Rainmaker52 's example is exactly what the doctor ordered. let's get it in the wiki.

It would be ideal to also include the actual command line that is being sent to the application.
I've been scratching my head trying to figure out why the most basic of items won't work, and I can't find a 'super duper happy path' example that actually shows me the proper input string so that I can use it in a unit test.

Was this page helpful?
0 / 5 - 0 ratings