0

In my command line interface like app, I want to make a detection if there was something written after the "command". One example is that if I wanted to make a directory with the "mkdir" command in my app, I want to know what is after it, like if I wrote "mkdir Directory", the mkdir char and whatever is after it are 2 different arguments, and I would make the directory that has the name of whatever the second argument is. I know it is wordy, but I am not the best at wording things. I have searched up on how to do it, but I can't word things well. this is my code:

    char command[] = "";
    char args[] = "";
    if(command=="mkdir"){
          //do what I want it to do which is to tell if args are put after command
    }
    else{
    cout << "The command does not have the right parameters. Sorry."
    }
    cout << "A:/>";
    cin >> command;
LeninYT
  • 1
  • 3
  • I am checking it out right now Pepjin Kramer, and I think it may work, but I got to test it out right now. – LeninYT Jan 22 '22 at 05:45
  • Then just check argc==2 and construct a std::string from argv[1] and compare against : "mkdir" – Pepijn Kramer Jan 22 '22 at 05:49
  • Thanks, I finished lessons on C++ a week ago and wanted to create something simple as this. – LeninYT Jan 22 '22 at 05:51
  • A cool :) You could also have looked here : https://www.learncpp.com/cpp-tutorial/command-line-arguments/ (the whole site is pretty decent) – Pepijn Kramer Jan 22 '22 at 05:53
  • The site you put will help me. :) – LeninYT Jan 22 '22 at 05:56
  • **(1)** `char command[] = "";` creates a `char` array of length 1; it's equivalent to `char command[1] = {'\0'};` The only thing you can read into this array without overflowing is an empty string. **(2)** `command=="mkdir"` will always be false. You are comparing two pointers, not the characters they point to, and `command` can't possibly have the same address as a string literal. In C++, use `std::string` for your string handling needs. **(3)** `cin >> command;` reads up to the nearest whitespace. If you type in `mkdir Directory`, it will only read `mkdir`. Unclear if that's what you meant. – Igor Tandetnik Jan 22 '22 at 16:02
  • @PepijnKramer There might be some misunderstanding. It appears the OP doesn't want to parse command arguments passed to their program. Rather, they are writing a sort of shell where the user types in a command line, and they want to parse that command line. `argc` and `argv` are not given (and so the code can't "just check" them), they are precisely what the program needs to compute. – Igor Tandetnik Jan 22 '22 at 16:07

0 Answers0