C ++: how to use command line arguments

I know that to use command line arguments I have to do this.

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

Now, most of the documentation I read about accepting command line arguments explains the situation something like this:

Command line arguments are specified after the program name in command line operating systems, such as DOS or Linux, and are transferred to the program from the operating system.

So, the only way I know to open my program is to open it normally, as I would, either start debugging or open an exe file

Now, it seems that to use command line arguments, you need to open the program in different ways using Command-Line (for example, DOS / Command Prompt), and then write the arguments after it.

So my question is:

How can I open my program using the command line and how can I enter arguments after the program name?

+4
source share
9 answers

For simplicity's sake, I assume you are using Windows 7.

The easiest way is to open a DOS window and then drag the application onto it. This will add the path to your executable file. After that, you can start typing the command line arguments you want to pass. It should look something like this:

 C:\Users\cscott> "C:\Users\cscott\Documents\myApp.exe" argument1 argument2 

Note. As mentioned in the comments, this does not work in Windows Vista, which I did not know at the time of writing.

+5
source

I assume that you are using an IDE, and I will assume that this is Visual Studio. If I'm right, there are two approaches: one, open the folder containing the executable file that was created - by default it will be in {Solution Directory} / {Project Directory} / bin / {Build Configuration}. Run the command line there. Another option is to open the project properties, and on the Debug tab (in VS 2010 - it depends on the version) place the command line flags in the "Command line arguments" field.

+4
source

Some ways to pass arguments to a program:

  • Open command prompt (e.g. cmd.exe or PowerShell on Windows), then type: your_program.exe arg1 arg2 arg3 .
    You can do the same in a shortcut or script (e.g. package or sh script).

  • Edit the launch configuration in your IDE.
    For example, Eclipse allows you to set command line arguments separately for each launch configuration. This is useful when developing and debugging.

  • On Windows, drag and drop the file into the executable file. The shuffled file name of the file will be passed as a command line argument.

  • On Windows, associate the file name extension with the filetype ( assoc ) type and map this file type to the command that runs your program ( ftype ). Now, when such a file is opened, either in the shell, or, for example, double-clicking what happens behind the curtains, is that your program starts from the path to this file as an argument.

  • Run the executable programmatically from another program and pass the arguments as variables.
    For example, in Python:
    subprocess.call(['my_program.exe','arg1','arg2'])

+2
source

On Windows, you must navigate through the command line to your executable location, and you can start it by saying Myexe.exe first_arg second_arg .

Alternatively, you can right-click your exe file, and in the file settings you can specify some command line arguments to provide it when it opens by double-clicking.

Another way is to create a simple batch of script that simply calls your program, for example C:/Full/Path/To/Your/Program/app.exe first_arg second_arg , and starts it.

In Visual Studio or your preferred IDE, you will have an option in the project settings to specify some command line arguments for your program when executed from within the IDE.

+1
source

Here is a simple example that I use on linux

 ./myprogram args1 args2 

and u can parse it like this:

 int main (int argc, char **argv) { if (argc >= 2) { std::string param(argv[1]); } ///etc } 
+1
source
 <path of your program> <Arguments separated by space> 
0
source

You can do this by opening a command prompt and cd for the path and enter the exe name followed by your options:

  eg: bob.exe bob dylan 

where your exe is bob and the two parameters are bob and dylan ...

... or you can make a shortcut and right-click, select properties, shortcut and add parameters to the end of the target field.

 "C:\bob.exe" /bob dylan 

There may be an option in your IDE, depending on what it is.

0
source

You can write at startup from the command line, you can create shortcuts and add arguments after the name, you can add arguments to some IDEs when debugging, or you can drop your program using another program using some arguments.

0
source

Something will start your program. This is for some reason pass on his arguments. All ordinary shells will parse the command line (though not always the same) to present you with arguments. In Windows, left-clicking on the buttons on the desktop and in the task bar will open a configuration window in which there is a tab "Shortcut" where you can enter the command line as "Target:". Unix rc files (executed at startup) are basically shell scripts, and cron files (startup time) also accept a command line (not just an isolated command). And so on.

In other contexts, you can map the type of file (extension) to the command that will be executed when you click on a file of this type or upload. In such cases, if nothing else, you will at least get the full path to the file.

In the few cases where you can only get the file name, it is quite easy to write your own shell script to add additional arguments when it calls your program.

0
source

Source: https://habr.com/ru/post/1432516/


All Articles