0

I have an assignment where My program is to be able to take one optional command line argument, the -d flag. And if this flag is entered, it is to change my global variable, bool debugMode from 0 to 1. I was told it would be similar to this :

int main (int argc, char** argv)

But I don't know how to properly implement it so that when it is ran with -d, it changes the bool from false to true.

My global variable is bool debugMode = 0 if that is worth mention.

Nukodi
  • 335
  • 1
  • 9
  • 24
  • Look up `[c] getopt` on SO; there will be many examples. – Jonathan Leffler Sep 18 '16 at 23:28
  • `if(argc > 1) debugMode = 1`; – Weather Vane Sep 18 '16 at 23:30
  • You should be using `bool debugMode = false;`, should you not? And then setting it to `true`. – Jonathan Leffler Sep 18 '16 at 23:31
  • 1
    @WeatherVane I am to check that they put in -d though, not just an argument – Nukodi Sep 18 '16 at 23:31
  • You said "one optional argument" so either it's there or it is not. – Weather Vane Sep 18 '16 at 23:32
  • The question I chose as a duplicate shows a boolean option in the top-voted answer. Another question that would help similarly is [Pass arguments into C program from command line](http://stackoverflow.com/questions/498320/). – Jonathan Leffler Sep 18 '16 at 23:32
  • @JonathanLeffler what library would i include besides stdbool.h to make it? Because my compiler underlined it when I made it false. – Nukodi Sep 18 '16 at 23:32
  • @WeatherVane: No; if the argument is `epistemology`, then it is not `-d` and an error message is in order. Similarly, it might be `-h` or something else that is not `-d`. – Jonathan Leffler Sep 18 '16 at 23:33
  • Sorry about my simplification it's getting late here. – Weather Vane Sep 18 '16 at 23:35
  • If you're compiling with a C compiler, `` is all you need to get `bool`, `true`, `false` defined. If it doesn't define them, then there is a serious problem. I'm blithely assuming you're compiling with C99 or later since you said you're using `bool`. Indeed, if you have `bool`, you should have `true` and `false` always, unless you are somehow defining `bool` for yourself. – Jonathan Leffler Sep 18 '16 at 23:35
  • @JonathanLeffler it works now with false, I'm not sure why it underlined it before. – Nukodi Sep 18 '16 at 23:37
  • @JonathanLeffler I also don't really understand what the first answer is doing in the link you posted. – Nukodi Sep 18 '16 at 23:38
  • Hmmm...the relevant bit is: `bool isCaseInsensitive = false; int opt; … while ((opt = getopt(argc, argv, "ilw")) != -1) { switch (opt) { case 'i': isCaseInsensitive = true; break; …` —— and that has a Boolean variable `isCaseInsensitive` that is initialized to `false`, and then a standard loop using the `getopt()` function to test the flag `i` (you'd want a `d` in the string passed to `getopt()`, and `case 'd':` in the switch), with a switch which spots when `-i` is passed (because `getopt()` returns `i`) and sets `isCaseInsensitive` to `true`. What's the complication you have problems with? – Jonathan Leffler Sep 18 '16 at 23:42
  • @JonathanLeffler The things that were confusing me the most were the switch cases and the stuff happening in them – Nukodi Sep 18 '16 at 23:49
  • OK. Well, you should look at the POSIX specification for [`getopt()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getopt.html) sometime (and then the GNU version of [`getopt()`](https://www.gnu.org/software/libc/manual/html_node/Getopt.html)). The key point is that you list the options in a string passed as the third argument to `getopt()` with a colon after the option letter if the option takes an argument (so you need `"d"` as the third argument), and `getopt()` returns either the option letter or an indicator of 'no more options' (`-1`) or an error. The switch analyzes this. – Jonathan Leffler Sep 18 '16 at 23:56
  • `bool debugMode = (argc == 2 && strcmp(argv[1], "-d") == 0);` – BLUEPIXY Sep 19 '16 at 00:06

0 Answers0