4

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 taking in command line arguments explain the situation, something like this:

Command-line arguments are given after the name of a program in command-line operating systems like DOS or Linux, and are passed in to the program from the operating system.

So the only way i know to open my program, is to open it normally like i would do, either start debugging or open the exe file

Now here it seems that, to use command line arguments the program has to be opened differently, Using the Command-Line (Windows Command Prompt for example), and then write the arguments after it.

So my question is

How do i open my program using the Command-Line, and how do i enter the arguments after the program name?

1
  • 2
    Mildly unrelated but you should investigate the Boost Program_options library if you need to do any sort of heavy lifting in your command line parsing. Makes reordering and defining functionality for flags very trivial. Commented Sep 5, 2012 at 15:55

9 Answers 9

6

For the purpose of simplicity, I will assume you are using Windows 7.

The simplest way is to open a DOS box and then drag-n-drop you application on to it. This will insert the path to your executable. Following that, you may begin typing the command line arguments you wish to pass it. It should end up looking something like this:

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

Note: As mentioned in the comments, this does not work on windows vista, a fact I was unaware of at the time of writing.

11
  • 2
    If you are typing it yourself (and there are no spaces in any of your folders), you do not need the quotation marks, but if you drag and drop it into the prompt, it will automatically insert them, just in case.
    – Tsubashi
    Commented Sep 5, 2012 at 15:53
  • Why do you rewrite this part C:\Users\cscott Commented Sep 5, 2012 at 15:54
  • You would not. I was just showing you what it would look like in my command prompt.
    – Tsubashi
    Commented Sep 5, 2012 at 15:54
  • So if i was manually writing it, Should i start like this \Documents\myApp.exe or like this Documents\myApp.exe Commented Sep 5, 2012 at 15:58
  • 1
    The C:\Users\cscott> part tells me what directory I am currently in. I can change this by typing cd directory_name. So, I could also type cd Documents and then myApp.exe arg1 and get the same result.
    – Tsubashi
    Commented Sep 5, 2012 at 16:12
4

I'm going to assume you're using an IDE, and I'll take a wild guess that it's Visual Studio. If I'm right, there are two approaches - one, open up the folder containing the executable that's been built - it'll be in {Solution Directory}/{Project Directory}/bin/{Build Configuration} by default. Run the command line there. The other option is to open the project properties, and under the "Debug" tab (in VS 2010 - it varies by version) put your command line flags in the box labeled "Command line arguments".

2

Some ways how arguments can be passed to a program:

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

  • Edit the run configuration in your IDE.
    For instance, Eclipse alllows you to set command-line arguments separately for each run configuration. This is helpful during development and debugging.

  • On Windows, drag and drop a file onto the executable. The dragged file's filename will be passed as a command-line argument.

  • On Windows, associate a filename extension with a filetype (assoc command) and associate that filetype with a command that runs your program (ftype command). Now when such a file is opened, either in the command interpreter or by e.g. double clicking, what happens behind the scenes is that your program is run with the path to that file as argument.

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

5
  • 1
    In Visual Studio 2008, command arguments are in the Project Properties->Configuration Properties->Debugging->Command Arguments. (Omit the filename there) Commented Sep 5, 2012 at 20:52
  • @MooingDuck I follow the same procedure as you suggested , but how to enter path for folder , for my folders of different classes , like D:\oranges\; for one folder and other D:\mangoes\; etc like this ?
    – Rocket
    Commented Sep 7, 2013 at 19:45
  • 1
    @Ahmad: I'm not sure what you're asking. If you want to pass multiple arguments, use "D:\oranges\" "D:\mangos\". Commented Sep 8, 2013 at 0:34
  • @MooingDuck there should be space between two arguments or ; ?
    – Rocket
    Commented Sep 8, 2013 at 12:02
  • 1
    @Ahmad: command line arguments are separated by spaces. If an argument contains a space, wrap it in quotation marks. This is clearly demonstrated on every answer on this page. Commented Sep 8, 2013 at 17:58
1

In Windows you have to navigate using the command prompt to your executable location and you can run 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 to it when it is opened by double clicking.

Another way is also by writing a simple batch script that just calls your program like C:/Full/Path/To/Your/Program/app.exe first_arg second_arg and running that.

In Visual Studio or your preferred IDE you will have the option in the project settings to specify some command line arguments to your program when executing from inside the IDE.

1

Here's a simple example I use in 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
}
0
<path of your program> <Arguments separated by space>
0

You can do this by either opening a command prompt and cd to the path and enter the exe name followed by your params:

 eg: bob.exe bob dylan

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

...or you can make a shortcut and right click, choose properties, shortcut and add the params to the end of the target field.

"C:\bob.exe" /bob dylan

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

0

You can write when launching from command prompt, you can make shortcut and add arguments after name, you can add arguments in some IDE when debugging or you can cal your program with other program using some arguments.

0

Something is going to start your program. It's up to that something to pass it arguments. All of the usual shells will parse the command line (although not always in the same way) to present you the arguments. Under Windows, left clicking on buttons on the desktop and in the task bar will open up a configuration window, which has a "Shortcut" tab where you can enter the command line as a "Target:". The rc files under Unix (executed on start-up) are basically shell scripts, and the cron files (timed start-up) also take a command line (and not just an isolated command). And so on.

In other contexts, you can map the file type (extension) to a command which will be executed when you click on a file of that type, or download it. 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 name of the file, it's fairly easy to write your own shell script to add extra arguments when it invokes your program.

Not the answer you're looking for? Browse other questions tagged or ask your own question.