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
You will have to compile the program.
The command line interface (CLI) options are passed to the function
main
: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):
Of course, the code needs to be compiled before you can run it.
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":
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
.