skip to Main Content

I am switching from a GitHub workspace to VS code.

I still have to make C code with command line arguments. The command would look something like ./code input output

How do I do this when I need to be able to keep switching both arguments regularly?

3

Answers


  1. You will have to compile the program.

    The command line interface (CLI) options are passed to the function main:

    int main (int argc, char const *argv[]) {
     // ...
    }
    

    At which point you would test the CLI argument in argv for what you need.

    i.e. (didn’t test this, as I’m writing directly to the web):

    int main(int argc, char const *argv[])
    {
      for (int i = 0; i < argc; ++i)
      {
        printf("CLI option %d: %s", argv[i]);
      }
    
      if(argc != 3) {
        fprintf(stderr, "missing input / output arguments.");
        exit(-1);
      }
    
      const char * input = argv[1];
      const char * output = argv[2];
    
      // ... 
      return 0;
    }
    

    Of course, the code needs to be compiled before you can run it.

    Login or Signup to reply.
  2. if you want to go "raw", you can use"int argc, char **argv", but if you want an easy solution, you can chose betwen use the native lib getopt, wich i consider hard, or use an "more high level lib", like C-Cli-Entry, these is my lib, and I dont know if its consider self promotion, but any way:

    first:
    Go to the repo:
    https://github.com/OUIsolutions/C-Cli-Entry

    Second:
    Download the entire ""CliEntry.h" file to your project

    Third:
    Now you can retrive any argv flags and args , in these case, I am simulating getting the output flag argument, acessed by: "./program -o file.txt":

    
    #include "CliEntry.h"
    
    
    int main(int argc, char **argv){
    
        CliNamespace cli = newCliNamespace();
    
    
        CliEntry *entry = newCliEntry(argc,argv);
        //get the flag
        CliFlag *output_flag = cli.entry.get_flag(entry,"o | out | output",CLI_NOT_CASE_SENSITIVE);
       
        //verify if exist
        if(output_flag->exist == false){
            printf("you didint passed the outputn");
            cli.entry.free(entry);
            return 1;
        }
        //verifying the size
        if(output_flag->size == 0){
            printf("the output flag its emptyn");
            cli.entry.free(entry);
            return 1;
        }
        int position = 0;
        char *output_content = cli.flag.get_str(output_flag,position,CLI_CASE_SENSITIVE);
        printf("content of output:%sn",output_content);
    
        //do stuff here 
    
        cli.entry.free(entry);
        return 0;
    }
    
    
    Login or Signup to reply.
  3. Visual Studio Code is not integrated with a compiler so you won’t be able to pass arguments in an easy way.

    You can embark yourself in a quest of installing the compiler following the instructions here https://code.visualstudio.com/docs/languages/cpp .

    Keep in mind that installing a compiler is not easy if you are a beginner.

    An easier way would be to install Visual Studio Community and use a C++ Console App template for creating a project. Then , from Visual Studio you can add arguments like this: Debug->Debug Properties->Configuration->Debugging->Command Line Arguments .

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search