• Articles
  • how can i use command line in my applica
Published by
Jul 9, 2013 (last update: Aug 25, 2013)

how can i use command line in my application?

Score: 2.9/5 (76 votes)
*****

what is the command line

sometimes, your application must do something
for an example, you want to use a feature with just one command
or call a function with one command and some parameters
or you want to get an input from user or you want to get an option that ask's remove the file or not
and many thinks that maybe you need them
you can do them by command line

intruduction to command line

command line is a window that user type's commands in it and computer does them
so, a command is an instruction
commands executed line by line
when you type a command and press enter, it will be executed
for example this is a command:
 
yourapp -h

we call help in this command
but an application may use another command for help or some other commands for do something

how to use command line in the programs

well, it is so easy!
you can use them by using Standard C and C++ functions like printf, scanf, etc
this is an example that show's how to add an option to specify the help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
using namespace std;
void help();
int main(int argc, char** argv)
{
if(strcmp(argv[0], "-h"))
{
help();
}
}
void help()
{
cout<<"-h for help"<<endl;
}

lets translate the code
first, we included the header files
after that we use the std namespace
we create the help function for specifying -h
in main function, we have 2 parameters
argc as a type of int
and argv is array pointer to char
notice that we can use char* argv[] instead of char** argv
in the if expression, we use strcmp that take's 2 parameters
boath of them are string
we use argv[0] for accessing first index of the array
at second, we specify the option to be used for help
in the if statement we call help function
and at last, we write help message in the help function

how to get an input file from user

this example show's how to get a file from user and open it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
void help();
int main(int argc, char**argv)
{
string input;
if(strcmp(argv[0], "-h")
{
help();
}
else if(strcmp(argv[0], input))
{
file *f=fopen(input, "rb");
}
}
void help()
{
cout<<"usage: <filename>"<<endl;
}

we created one option(file input) that get's an input from user and open's it
in main function:
we created one string called input
we added help message by using "-h"
then in the if statement, we pass input as the second parameter
then, we use fopen() to open that file

notes

you can access other indexes of argv
argc is the number of options that user specify's
so, argv[argc-1] is the last index of the array
but when argc=1, argv[0] specify's
this is because of the number of parameters that user specify's
and argv[0] is the first index
at last,but not leest, you can limit the number of parameters that user specify's by adding another if statement in main function like this:
1
2
3
4
if(argc==4)
{
help();
}

as you can see, parameters limited to 4
but you can limit it to another number