command line switches

I have been trying to develop a program that will calculate distances, mass, volume and others, but I am stuck at programming the switches, you know like in this:
ping google.com -t -l 500
I have created the batch file that will create the classpath and path variables. I just have to get something like:
calc volume -u mm -l 500 -w 333 -h 7853
I have no idea how to do that, is it short, or is it tons of lines?
Last edited on
to get the program arguments, use argc and argv:
1
2
3
4
int main ( int argc, char **argv )
{
    // you can use argc, argv in here
}

argc is the number of arguments passed
argv is an array of C strings holding the argument values
argv[0] is always the name of your program
argv[i] is the ith argument
I get what you say, but is this how you do it:
1
2
3
4
5
6
7
8
9
int main ( int argc, char **argv )
{
     //for first input after name (volume)
     if (**argv[1] == "millimeters")
     {
          //code
     }
     //etc.
}

or do you put just argv[1], and do you get letters by doing argv[w]?
 
if ( argc >= 2 && std::string ( argv[1] ) == "millimeters" )

( You should check your arguments in a loop )
is my way correct,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// calc.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

void volume_calc(double l, double w, double h)
{
    cout << "not running" << endl;
}

void length_calc(double ang)
{
    cout << "not running" << endl;
}

void angle_calc(double dist)
{
    cout << "not working" << endl;
}

void help_calc()
{
	cout << "HELP:\n"
		<< "+-------------+-----------------------------------------------+\n"
		<< "|FIND VOLUME  |calc volume -l [length] -v [width] -h [height] |\n"
		<< "|-------------|-----------------------------------------------|\n"
		<< "|FIND LENGHT  |calc length -a [angle of connection]           |\n"
		<< "|-------------|-----------------------------------------------|\n"
		<< "|FIND ANGLE   |calc angle -d [distance between ray 1 and 2]   |\n"
		<< "+-------------+-----------------------------------------------+\n"
		<< endl;
}

int main( int argc, char **argv )
{
    if (argv[1] == "volume")
    {
        volume_calc(3.00,5.00,6.00);
    }
    if (argv[1] == "length")
    {
        length_calc(3.456);
    }
    if (argv[1] == "?")
    {
        help_calc();
    }
}
This won't work:
 
if (argv[1] == "volume")

To compare cstrings you need to use the function strcmp();
Or you could convert the args to an array of std::strings.

-Albatross
I did that, but I cannot get things like
-t -l -v -atr -i -cant -get -this -to -work
if tried to put, argv[v] and like that, and I cannot get it to work
Tell me how to do the -l -t and where to put it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// calc.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

void volume_calc(double l, double w, double h, std::string unit)
{
    double lwh = l*w*h;
	cout << lwh << " " << unit << endl;
}

void length_calc(double ang)
{
    cout << "not running" << endl;
}

void angle_calc(double dist)
{
    cout << "not working" << endl;
}

void help_calc()
{
	cout << "HELP:\n"
		<< "+-----------+----------------------------------------------+\n"
		<< "|FIND VOLUME|calc volume -l [length] -v [width] -h [height]|\n"
		<< "|           |     -u [unit of measurement]                 |\n"
		<< "|-----------|----------------------------------------------|\n"
		<< "|FIND LENGHT|calc length -a [angle of connection]          |\n"
		<< "|           |     -u [unit of measurement]                 |\n"
		<< "|-----------|----------------------------------------------|\n"
		<< "|FIND ANGLE |calc angle -d [distance between ray 1 and 2]  |\n"
		<< "|           |     -u [unit of measurement]                 |\n"
		<< "+-----------+----------------------------------------------+\n"
		<< endl;
}

int main( int argc, char **argv )
{
	argv[l];
	argv[w];
	argv[h];
	if ( argc >= 2 && std::string ( argv[1] ) == "volume" )
    {
        volume_calc(3.00,5.00,6.00,"mm");
    }
    if ( argc >= 2 && std::string ( argv[1] ) == "length" )
    {
        length_calc(3.456);
    }
    if ( argc >= 2 && std::string ( argv[1] ) == "/?" )
    {
        help_calc();
    }
	if ((argc = 4 && double (argv[l]) > 0.00) && (double ( argv[w] ) > 0.00) && (double (argv[h]) > 0.00)
	{
		
	}
}
Try something like this:

1
2
3
4
5
6
7
int main(int argc,char* argv[]){
	//compare the strings - make sure the value is logically negated
	//because strcmp returns 0 when they're the same
	if(!strcmp(argv[1],"-myswitch")){
		/*do stuff*/}
	return 0;}
As I said, you should use a loop.
Otherwise you must check that argc is large enough before subscripting argv
You can loop through your args like this:
1
2
3
4
5
6
7
8
9
10
11
int main(int argc,char* argv[])
{
    // args start at 1 (0 is program name)
    for(int i = 1; i < argc; ++i) // index every arg in argv[]
    {
        if(std::string(argv[i]) == "-w") // test each arg against our switch
        {
            // found -w ... 
        }
    }
}


Last edited on
Topic archived. No new replies allowed.