Parsing Command Line Parameters

Hello everyone,

I'm looking for anyone who can explain how the following code works. This is an assignment for a data structures class that I am currently in. I don't want someone to give me code but just an explanation as to how and why it works. The assignment is to parse command line arguments in UNIX. It deals with some topics such as classes, structures etc... that I have not seen for awhile. Any and all help would be greatly appreciated. All of the code has came from the instructor and what he wants the class to do is to use the code he has provided and make a basic calculator, but as stated above, I am completely lost on how his code works. Thank you for your time.

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
begin file: cmdParser.h

#ifndef PARSER_H
#define PARSER_H
#include <string>

typedef struct argument
{
   std:: string flag;
   std:: string value;
}Argument;

class CmdParser
{
   public:
      CmdParser(int argc, char **argv);
      Argument nextArgument();
      bool hasNext();
   private:
      int argc, index;
      char **argv;
};

end of file cmdParser.h

begin file cmdParser.cpp
#include "cmdParser.h"
#include <string>
using namespace std;

CmdParser::CmdParser(int argc, char **argv)
{
   this->argc = argc;
   this->argv = argv;

   index = 1;
}

Argument CmdParser::nextArgument()
{
   Argument result;

   if(index >= argc)
      return result;

   if(argv[index][0] != '-')
   {
      //handle null flags
      result.value = argv[index];
   }
   else
   {
      //this is a flag
      if(argv[index][1] == '-')
      {
         //handle the multiple character flag
         result.flag = argv[index]] + 2;
      }
      else
      {
         //handle single character flag
         result.flag = argv[index][1];

         //there is a chance the value part is in the same string
         if(argv[index][2] != '\0')
            result.value = argv[index] + 2;
      }
   }
}

//advance the index
index++;

if(result.value.empty() && index < argc && argv[index][0] != '-'
{
   result.value = argv[index];
   index++;
}

return result;
}

bool CmdParser::hasNext()
{
   return index < argc;
}

end of file cmdParser.cpp
      
begin testCmdParser.cpp
#include <iostream>
#include <string>
#include "cmdParser.h"

using namespace std;

int main(int argc, char **argv)
{
   CmdParser args(argc, argv);
   Argument argument;

   while(args.hasNext() )
   {
      argument = args.nextArgument();

      if(!argument.flag.empty() )
      {
         cout << argument.flag << ": ";
      }

      cout << argument.value << endl;
   }

   return 0;
}

end of file testCmdParser.cpp 

When I began to start coding the assignment, I noticed that I had to
#include "cmdParser.cpp"

From what I have read, this is very bad, but it is the only way that I could get my file to compile.   
Just include the header, but make sure you like the results of the source files together (if you are using an IDE, this usually involves adding them to a project of some kind).

As for the actual workings of the class, the most important part is in the nextArgument() method; try reading through that and explaining what is happening at each step and then try to figure out why.
Topic archived. No new replies allowed.