Command Line Arguments Dev C++

  1. C++ Read Command Line Arguments
  2. C++ Command Line Argument Library
  3. C Get Command Line Argument
  4. How To Add Command Line Arguments In Dev C++
13 Mar 2020CPOL
In this tip, I want to represent a lightweight quick-and-dirty possibility (one of many) for parsing command line arguments on the fly using C++17.

Mar 13, 2020  In this tip, I want to represent a lightweight quick-and-dirty possibility (one of many) for parsing command line arguments on the fly using C17. A quick way of parsing command line arguments in modern C-17 for non-production without using third party header files / libraries. Nov 10, 2019 However, we can also pass arguments to the main function of C which are known as Command Line Arguments. Command line arguments are given after the name of the program during the execution of the program in a command-line shell. In order to pass command line arguments, the main function is passed with two arguments. May 27, 2011 In this tutorial on C I want to show you the command line arguments. Actually we will give parameters to the main function.

A quick way of parsing command line arguments in modern C++-17 for non-production without using third party header files / libraries. For real world projects, a third party approach is more encouraged.

Introduction

This is intended for small single/couple-file playground-try-like apps.

Line

Background

Very often, by learning new libraries or creating small playground apps (for example, which fit in a single file or a couple of files), I need to parse command line arguments, but don't want mental/time overhead of importing whole libraries (like boost) or lookup third party header files just for that purpose. The work sequence in that scenario is something like this:

  1. Fire VIM, VS, XCode.
  2. Try out some code.
  3. Compile and run with passing some test command line arguments.
  4. If everything clear/OK, goto 2 otherwise goto 5.
  5. Do something else.

Using the Code

As always, when designing some feature or library, I usually go from the use-case first, and then look for appropriate implementation.

The use-case for parsing command line arguments, which I found most convenient for myself, looks something like this:

The MyOpts struct is holding the options which I intend to pass via command line. So in this case, we will have one string option, one int option and one bool option:

C++ Read Command Line Arguments

Now, those options should have some names. In this case, I want to name my string option opt1, the int option should be named opt2, and the boolean option should be named opt3. The command line in this case would then look like this:

So, the 'some-string-option' should be assigned to MyOpts::stringOpt, 33 should be assigned to MyOpts::intOpt and true should be assigned to MyOpts::boolOpt.

To achieve this, I came up with the following implementation for CmdOpts:

Command Line Arguments Dev C++

Now let's break this apart:

* CmdOpts is a class template. Its template parameter is type of an object which command line arguments will be assigned to. In the example above, it would be MyOpts.

* The MyProp type alias:

is conceptually a union (std::variant is a C++17 type safe union), where each template parameter is of type pointer to member of Opts. So, at every given timepoint, a variable of type MyProp will hold a pointer to exactly one member of Opts (in our example MyOpts). At this point, we are restricting us to 4 different types: string, int, double and bool. So we are basically saying: every command line argument we pass, will be converted to one of those 4 types. This isn't ideal and could certainly be improved, but remember: we want to keep our implementation minimalistic and simple. And for command line argument, passing those four types would normally be sufficient.

* MyArg will be holding a mapping between option names (for example --opt1) and members of our Opts object (for example, stringOpt), where the values from the command line will be assigned to.

* For every option we will define a callback, so when the option is encountered on the command line, the callback will execute and assign the value to the associated object. So for this, we are holding a mapping between option names and callbacks:

The callback is a function type, whose parameters are the current index of the command line arguments being passed and a view to all available command line arguments. This is needed to get the argument value:

CmdOpts::parse is the main function for our parser. It basically goes over the entire command line arguments and execute each callback in turn. The algorithmic complexity O(n2) is not an issue here, unless we expect to have a big number of command line args, which would be bad use anyway (input file would be more appropriate in this case).

CmdOpts::register_callback sets the callback for the option named name. If the option with this name is encountered, the value is read and written to object property prop. Note: we are using stringstream and variant visitor here, for doing the conversion for different types automatically: every type comfortable with standard inputoperator >> is ok. Note also that boolean input is expected 0 and 1 (instead of false/true).

C++ Command Line Argument Library

Command Line Arguments Dev C++

And that's it! Here's the complete source file for the above example:

Points of Interest

Further possible improvements, would be for example, to make the usage of custom input operators >> and generics for MyProp or to print usage information, but for those more complicated scenarios, it would be more advisable just to use the third party header files / libraries.

Can you come up with some better / more concise implementation, or have some better idea? If so, then please leave comments below.

History

  • 13th March, 2020: First version
  • C Programming Tutorial
  • C Programming useful Resources
  • Selected Reading

It is possible to pass some values from the command line to your C programs when they are executed. These values are called command line arguments and many times they are important for your program especially when you want to control your program from outside instead of hard coding those values inside the code.

The command line arguments are handled using main() function arguments where argc refers to the number of arguments passed, and argv[] is a pointer array which points to each argument passed to the program. Following is a simple example which checks if there is any argument supplied from the command line and take action accordingly −

When the above code is compiled and executed with single argument, it produces the following result.

When the above code is compiled and executed with a two arguments, it produces the following result.

When the above code is compiled and executed without passing any argument, it produces the following result.

C Get Command Line Argument

It should be noted that argv[0] holds the name of the program itself and argv[1] is a pointer to the first command line argument supplied, and *argv[n] is the last argument. If no arguments are supplied, argc will be one, and if you pass one argument then argc is set at 2.

How To Add Command Line Arguments In Dev C++

You pass all the command line arguments separated by a space, but if argument itself has a space then you can pass such arguments by putting them inside double quotes ' or single quotes '. Let us re-write above example once again where we will print program name and we also pass a command line argument by putting inside double quotes −

When the above code is compiled and executed with a single argument separated by space but inside double quotes, it produces the following result.