4

I have a program that does two separate things. But I want to the program to only run one specific part based on what I enter in the command like in the format

program -a
program -s

where the a or s would be programmed to tell it to do something specific.

For example, if my program was something like:

# program -a entered
z = x + y
# program -s entered
z = x - y

Now I could easily do this with an if statement after running the program, but is it possible to directly skip to a statement like I said from the command prompt?

  • 2
    No (you can't jump direct to the statement "from the command prompt"). You'll have parse the command line arguments and use an `if` of some sort to choose what to do, and how to report errors when the forgetful or recalcitrant user doesn't specify any arguments, or specifies both `-a` and `-s`, or specifies `-z` or any other invalid option, or specifies `program dwim` (your code probably won't Do What I Mean). Etc. – Jonathan Leffler Oct 09 '18 at 05:41
  • @JonathanLeffler would you be able to provide any example code on how I would implement that into a program? Or any readings? – mysterybands Oct 09 '18 at 05:45
  • 1
    Look up the POSIX [`getopt()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getopt.html) and the GNU [`getopt_long()`](https://www.gnu.org/software/libc/manual/html_node/Getopt.html#Getopt) functions. There are many alternatives, too. For your immediate needs, plain old `getopt()` is adequate. For example, see [Parsing command line arguments](https://stackoverflow.com/questions/9642732/), and there are lots of others. – Jonathan Leffler Oct 09 '18 at 05:47
  • While it is uncommon and running somewhat against the C paradigm [there are C interpreters out there](https://stackoverflow.com/questions/584714/is-there-an-interpreter-for-c). The one I worked with (a proprietary embedded debug environment not mentioned there) allowed us to evaluate specific expressions, including function calls, from the command line. If you put the two code paths in two separate functions you could call them. – Peter - Reinstate Monica Oct 09 '18 at 06:08
  • How do you mean "with an if statement after running the program"? The fact that you are asking for example code for "use an if of some sort to choose what to do" seems to imply that you have no idea of how to do that. So there must be a difference between the two summaries. Also "after running" is confusing to me. I'd expect "use an if **before** running either part". – Yunnosch Oct 09 '18 at 06:10

2 Answers2

7
int main(int argc, char* argv[]) {
    if (argc >= 2) {
        if (strcmp(argv[1], "-a") == 0) {
            z = x + y;
        } else if (strcmp(argv[1], "-s") == 0) {
            z = x - y;
        }
    }

    return 0;
}

Determine if the value of argv[1] equal to "-a" or "-s"

sundb
  • 490
  • 2
  • 8
4

You can use a switch statement on command-line arguments (in this case argv[1]).
argv[1] is a string containing the options you have given ("-a", "-s" etc).

But you have to perform certain checks for the validity of the command-line arguments before you can use them like this.

  1. Check if argc == 2
  2. Check if strlen(argv[1]) == 2
  3. Check if argv[1][0] == '-'

An MCVE would look like this:

#include <stdio.h>
#include <string.h>

int main(int argc, char* argv[]) {

    if(argc == 2 && strlen(argv[1]) == 2 && argv[1][0] == '-')
    {
        switch(argv[1][1])
        {
          case 'a':
           //do something 
             break;
          case 's':
           //do something else
             break;
          default:
           //do the default thing 
             break;
        }
    }
    else
    {
        //print appropriate error message
    }   
}
P.W
  • 26,289
  • 6
  • 39
  • 76