Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. library for command-line parameters

library for command-line parameters

Scheduled Pinned Locked Moved C#
csharpdatabasecomjsonhelp
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lukeer
    wrote on last edited by
    #1

    Hello Experts, one of the console applications I wrote is about to get modified to do a similar job to the one it was first designed to do. Its command-line parameters parsing works. But I never have been really happy with it. This example

    tool.exe SetDo FirstLed On

    should make an attached device switch its first LED on, obvisously. And it does so. What bothers me is that SetDo has to be the first parameter, FirstLed the second and On the third. No way for the user to put an option before SetDo. Since the modifications will change the possible actions anyway, I thought of finding a more standard library instead of using my own parsing. First I checked the GNU getopt .NET port[^] and found using it horrible. You have to specify a format string, only for one-character options and a switch statement that matches the format string. Then you can attach long options that use a short option to determine what action they trigger. That approach appears to be inspired by sprintf's format string[^], which we all know about and that it works. It just is so unintuitive to use from a programmer's point of view. My second try was Mono.Options[^]. It doesn't require a format string. You just have to fill a generic list with one object per supported command. Short and long option names are supported in the object's constructor, along with a description of the command (for the auto-generated help screen) and a method to call during runtime. Now I recognize that there's still a lot of work to do by hand since not all my options are boolean switches. The example I gave above uses two enumerations, one that holds all possible digital outputs to manipulate, the other for the state that should be the result of the manipulation. It doesn't look like there is an easy way to get that into the auto-generated help file, nor that the parser can handle situations like "Option A must preceed one of enum B's values followed by enum C's value

    P 2 Replies Last reply
    0
    • L lukeer

      Hello Experts, one of the console applications I wrote is about to get modified to do a similar job to the one it was first designed to do. Its command-line parameters parsing works. But I never have been really happy with it. This example

      tool.exe SetDo FirstLed On

      should make an attached device switch its first LED on, obvisously. And it does so. What bothers me is that SetDo has to be the first parameter, FirstLed the second and On the third. No way for the user to put an option before SetDo. Since the modifications will change the possible actions anyway, I thought of finding a more standard library instead of using my own parsing. First I checked the GNU getopt .NET port[^] and found using it horrible. You have to specify a format string, only for one-character options and a switch statement that matches the format string. Then you can attach long options that use a short option to determine what action they trigger. That approach appears to be inspired by sprintf's format string[^], which we all know about and that it works. It just is so unintuitive to use from a programmer's point of view. My second try was Mono.Options[^]. It doesn't require a format string. You just have to fill a generic list with one object per supported command. Short and long option names are supported in the object's constructor, along with a description of the command (for the auto-generated help screen) and a method to call during runtime. Now I recognize that there's still a lot of work to do by hand since not all my options are boolean switches. The example I gave above uses two enumerations, one that holds all possible digital outputs to manipulate, the other for the state that should be the result of the manipulation. It doesn't look like there is an easy way to get that into the auto-generated help file, nor that the parser can handle situations like "Option A must preceed one of enum B's values followed by enum C's value

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      There are many command line parser articles available on this site. The following is just a sample that a simple search found:
      Commander - A Command Parser[^]
      C#/.NET Command Line Argument Parser Reloaded[^]
      C# command line parsing[^]
      Advanced command line parser class for .NET[^]
      C#/.NET Command Line Arguments Parser[^]
      Intelligent Command Line Parser[^]
      Simple Command Line Parser[^]
      CCmdLine - A command line parser[^]
      Lightweight C# Command Line Parser[^]
      Powerful and simple command line parsing in C#[

      1 Reply Last reply
      0
      • L lukeer

        Hello Experts, one of the console applications I wrote is about to get modified to do a similar job to the one it was first designed to do. Its command-line parameters parsing works. But I never have been really happy with it. This example

        tool.exe SetDo FirstLed On

        should make an attached device switch its first LED on, obvisously. And it does so. What bothers me is that SetDo has to be the first parameter, FirstLed the second and On the third. No way for the user to put an option before SetDo. Since the modifications will change the possible actions anyway, I thought of finding a more standard library instead of using my own parsing. First I checked the GNU getopt .NET port[^] and found using it horrible. You have to specify a format string, only for one-character options and a switch statement that matches the format string. Then you can attach long options that use a short option to determine what action they trigger. That approach appears to be inspired by sprintf's format string[^], which we all know about and that it works. It just is so unintuitive to use from a programmer's point of view. My second try was Mono.Options[^]. It doesn't require a format string. You just have to fill a generic list with one object per supported command. Short and long option names are supported in the object's constructor, along with a description of the command (for the auto-generated help screen) and a method to call during runtime. Now I recognize that there's still a lot of work to do by hand since not all my options are boolean switches. The example I gave above uses two enumerations, one that holds all possible digital outputs to manipulate, the other for the state that should be the result of the manipulation. It doesn't look like there is an easy way to get that into the auto-generated help file, nor that the parser can handle situations like "Option A must preceed one of enum B's values followed by enum C's value

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        No other responses? Having given it some more thought... If I understand you correctly, you are using Enumerations for each parameter. Why not put them all in one enumeration?

        D 1 Reply Last reply
        0
        • P PIEBALDconsult

          No other responses? Having given it some more thought... If I understand you correctly, you are using Enumerations for each parameter. Why not put them all in one enumeration?

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Why? You covered it with that huge pile of links to various libraries!

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak

          P 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Why? You covered it with that huge pile of links to various libraries!

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            I'd still prefer to see some discussion.

            D 1 Reply Last reply
            0
            • P PIEBALDconsult

              I'd still prefer to see some discussion.

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              OK, cool. I'd layout the command line format like this, following the old DOS spec of everything inside square brackets being optional:

              tool.exe \[\[/option\] \[value\]\] \[/cmd "targetNoun actionVerb value"\]
              

              That would make it easier to parse as each switch is either a "Named Switch" or a "Named Value". The CMD switch could even be extended like this:

              /cmd "targetNoun actionVerb \[value\]\[;...\]"
              

              making it easier to parse for multiple commands to execute in the specified order.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak

              P 1 Reply Last reply
              0
              • D Dave Kreskowiak

                OK, cool. I'd layout the command line format like this, following the old DOS spec of everything inside square brackets being optional:

                tool.exe \[\[/option\] \[value\]\] \[/cmd "targetNoun actionVerb value"\]
                

                That would make it easier to parse as each switch is either a "Named Switch" or a "Named Value". The CMD switch could even be extended like this:

                /cmd "targetNoun actionVerb \[value\]\[;...\]"
                

                making it easier to parse for multiple commands to execute in the specified order.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                I was thinking... TOOL action /LED=n|(list) /STATE=ON|OFF e.g. TOOL SET /LED=1 /STATE=ON TOOL SET /LED=(3,5,7) /STATE=OFF (But what other actions are there?) Or TOOL /ON=n|(list) | /OFF=n|(list) ... e.g. TOOL /ON=1 /OFF=(3,5,7) /ON=2

                D 1 Reply Last reply
                0
                • P PIEBALDconsult

                  I was thinking... TOOL action /LED=n|(list) /STATE=ON|OFF e.g. TOOL SET /LED=1 /STATE=ON TOOL SET /LED=(3,5,7) /STATE=OFF (But what other actions are there?) Or TOOL /ON=n|(list) | /OFF=n|(list) ... e.g. TOOL /ON=1 /OFF=(3,5,7) /ON=2

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  Yeah, we have no idea what else he's got to control, nor how he has to refer to them. The LED=n might work for him but it really depends on the complete list of items he needs to control and the verb and value options available for each of those. When he knows that, then he might be able structure a standard format command line if there are a lot of possibilities or, if not, simplify it to what you've said.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups