Parsnip
parsing library
Loading...
Searching...
No Matches
parsnip_argv.h
Go to the documentation of this file.
1
8
9#include <config.h>
10
11#include <string>
12#include <vector>
13
14namespace Parsnip {
19 class ArgumentVector: public std::vector <std::string> {
20 friend class ArgvCursor;
21 public:
22 using StringType = value_type;
23 private:
25 const StringType original;
27 std::vector <StringType::size_type> remainder_start;
28 public:
29 ArgumentVector (const StringType &command_line);
30
35 inline StringType remainder (size_type index) const {
36 assert (index >= 0 && index < size());
37 return original.substr (remainder_start [index]);
38 }
39 };
40
42 class ArgvCursor {
43 const ArgumentVector *argv {nullptr};
44 ArgumentVector::size_type point = 0;
45 public:
46 inline ArgvCursor (const ArgumentVector *the_argv): argv (the_argv) {
47 }
48 inline ArgvCursor (const ArgvCursor &from) = default;
49 inline ArgvCursor (ArgvCursor &&from) = default;
50 inline ArgvCursor &operator =(const ArgvCursor &from) = default;
51 inline ArgvCursor &operator =(ArgvCursor &&from) = default;
52
55 inline bool isStart() const {
56 return point == 0;
57 }
58
62 inline bool isEnd() const {
63 return point >= argv->size();
64 }
65
66 inline ArgvCursor &operator++() {
67 point++;
68 return *this;
69 }
70
71 inline ArgvCursor operator++(int) {
72 ArgvCursor prior { *this };
73 point++;
74 return prior;
75 }
76
77 inline ArgvCursor operator+(int value) const {
78 ArgvCursor result { *this };
79 result.point += value;
80 return result;
81 }
82
83 inline ArgvCursor operator-(int value) const {
84 assert (point > 0);
85 ArgvCursor result { *this };
86 result.point -= value;
87 return result;
88 }
89
91 inline const ArgumentVector::StringType &value () const {
92 assert (point >= 0 && point < argv->size());
93 return (*argv) [point];
94 }
95
97 inline const ArgumentVector::StringType remainingString () const {
98 assert (point >= 0 && point < argv->size());
99 return argv->remainder (point);
100 }
101 std::vector <ArgumentVector::StringType> remainingTokens () const;
102 };
103}
This class lexically analyzes a string, splitting it up into tokens, which are accessed as a vector.
Definition parsnip_argv.h:19
StringType remainder(size_type index) const
Retrieve the raw, untokenized remainder of the string.
Definition parsnip_argv.h:35
An iterator for argument vectors.
Definition parsnip_argv.h:42
bool isEnd() const
Check if the cursor is at the end of the command line.
Definition parsnip_argv.h:62
bool isStart() const
Check if the cursor is at the start of the command line.
Definition parsnip_argv.h:55
const ArgumentVector::StringType & value() const
Return the current token.
Definition parsnip_argv.h:91
const ArgumentVector::StringType remainingString() const
Return the raw, unsplit remainder of the command line.
Definition parsnip_argv.h:97
Serialization and parsing library.
Definition parsnip.h:38