Skip to content

Commit 8bf3fd7

Browse files
committed
Refactoring
1 parent 6e9689d commit 8bf3fd7

File tree

4 files changed

+438
-322
lines changed

4 files changed

+438
-322
lines changed

README.md

Lines changed: 144 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# BazPO - Program Options Argument Parser
1+
# **BazPO - Program Options Argument Parser**
22

3-
BazPO takes away all the hassle handling program arguments, all you need is a line per option!
3+
*BazPO takes away all the hassle handling program arguments, all you need is a line per option!*
44

5-
## Features
5+
## **Features**
66

77
- Developer friendly and easy to use with advanced features.
88
- Automatic value conversions.
@@ -13,85 +13,153 @@ BazPO takes away all the hassle handling program arguments, all you need is a li
1313
- Automatic program exit on invalid arguments when specified.
1414
- Type conversion extensibility
1515

16-
## Getting Started
16+
## **Getting Started**
1717

1818
- Download BazPO.hpp source code and add to your project folder
1919
- Include the downloaded header to your main program
2020
- Library will be compiled along with your program
2121
- **[Download BazPO Here](www.google.com)**
2222

23-
**Example (1):**
23+
### **Functions / Usage**
24+
25+
- Instantiate BazPO::Cli
26+
- Add your options
27+
- Call parse
28+
- Read your values
29+
30+
- **Adding an option**
31+
- Using add() [`see example 1`](#Example(1):)
32+
- Defining the option by yourself [`see example 2`](#Example(2):)
33+
34+
- **Reading the values**
35+
- Calling option(tag) with the tag you specified to get the option [`see example 1`](#Example(1):)
36+
- When using tagless options specify the order number as a string tag [`see example 2`](#Example(2):)
37+
- Calling value(tag)/values(tag) with the relevant tag to get option values directly [`see example 2`](#Example(2):)
38+
- Directly reading from the defined object [`see example 3`](#Example(3):)
39+
40+
### **Examples**
41+
42+
#### **Example (1):**
2443

2544
```c++
2645
#include "BazPO.hpp"
2746

2847
using namespace BazPO;
2948

30-
int main(int argc,const char* argv[])
49+
int main(int argc, const char* argv[])
3150
{
3251
Cli po(argc, argv);
3352

34-
po.Add("-a", "--alpha", "Option A", true);
35-
po.Add("-b", [&](const Option&) { /* do something */ }, "--bravo", "Option B");
53+
po.add("-a", "--alpha", "Option A", true);
54+
po.add("-b", [&](const Option&) { /* do something */ }, "--bravo", "Option B");
3655

37-
po.ParseArguments();
56+
po.parse();
3857

39-
auto aoption = po.GetOption("-a");
58+
auto aoption = po.option("-a");
4059

4160
std::cout << "EXISTS:" << aoption.exists() << std::endl;
42-
std::cout << "INT:" << aoption.value_as<int>() << std::endl;
43-
std::cout << "BOOL:" << aoption.value_bool() << std::endl;
61+
std::cout << "INT:" << aoption.valueAs<int>() << std::endl;
62+
std::cout << "BOOL:" << aoption.valueBool() << std::endl;
4463
}
4564
```
4665
47-
**Example (2):**
66+
- Output
67+
68+
```txt
69+
./myProgram
70+
<-a> is a required parameter
71+
72+
myProgram
73+
usage: myProgram <-a> [-b] [-h]
74+
Program Options:
75+
<-a> --alpha Option A
76+
[-b] --bravo Option B
77+
[-h] --help Prints this help message
78+
79+
./myProgram -a 255
80+
EXISTS:1
81+
INT:255
82+
BOOL:0
83+
```
84+
85+
#### **Example(2):**
4886

4987
```c++
5088
#include "BazPO.hpp"
5189

5290
using namespace BazPO;
5391

54-
int main(int argc,const char* argv[])
92+
int main(int argc, const char* argv[])
5593
{
5694
Cli po(argc, argv);
57-
po.Add(5);
58-
po.Add();
59-
po.ParseArguments();
95+
po.add(5);
96+
po.add();
97+
po.parse();
6098

61-
for(const auto& values : po.GetOption("0").values())
99+
for(const auto& values : po.option("0").values())
62100
std::cout << values << std::endl;
63101

64-
std::cout << po.GetOption("1").value() << std::endl;
102+
std::cout << po.option("1").value() << std::endl;
65103
}
66104
```
67105
68-
**Example (3):**
106+
- Output
107+
108+
```txt
109+
./myProgram input1 input2 input3 input4 input5 input6 input7
110+
Given value -> 'input7' is not expected
111+
BazPOManual
112+
usage: BazPOManual [-h] [(5)] [(1)]
113+
Program Options:
114+
[-h] --help Prints this help message
115+
116+
./myProgram input1 input2 input3 input4 input5 input6
117+
input1
118+
input2
119+
input3
120+
input4
121+
input5
122+
input6
123+
```
124+
125+
#### **Example(3):**
69126

70127
```c++
71128
#include "BazPO.hpp"
72129

73130
using namespace BazPO;
74131

75-
int main(int argc,const char* argv[])
132+
int main(int argc, const char* argv[])
76133
{
77134
Cli po(argc, argv);
78135

79-
ValueOption optionA(&po, "-a", "--alpha", "Option A");
136+
ValueOption optionA(&po, "-a", "--alpha", "Option A", true);
80137
FunctionOption optionB(&po, "-b", [&](const Option& option) {
81-
/* do something */
138+
/* do something */
82139
}, "--bravo", "Option B");
83140

84-
po.UserInputRequiredForAbsentMandatoryOptions();
141+
po.userInputRequired();
142+
143+
po.parse();
85144

86-
po.ParseArguments();
87-
88145
std::cout << "EXISTS:" << optionA.exists() << std::endl;
89-
std::cout << "INT:" << optionA.value_as<int>() << std::endl;
90-
std::cout << "BOOL:" << optionA.value_bool() << std::endl;
146+
std::cout << "INT:" << optionA.valueAs<int>() << std::endl;
147+
std::cout << "BOOL:" << optionA.valueBool() << std::endl;
91148
}
92149
```
93150
94-
## Options
151+
- Output
152+
153+
```txt
154+
./myProgram
155+
<-a> is a required parameter
156+
<-a>: True
157+
EXISTS:1
158+
INT:0
159+
BOOL:1
160+
```
161+
162+
## **Options**
95163

96164
### **ValueOption**
97165

@@ -123,9 +191,52 @@ myprogram -a value1 value2 value3 -a value4 -b
123191
myprogram value1 value2 value3 value4
124192
```
125193

126-
### **FunctionOption / FunctionMultiOption / FunctionTaglessOption**
194+
### **FunctionOption/FunctionMultiOption/FunctionTaglessOption**
127195

128196
- Provided function will be executed if the given tag is provided as an argument with or without a value.
129-
- FunctionOption is parsed like [`ValueOption`](###ValueOption).
130-
- FunctionMultiOption is parsed like [`MultiOption`](###MultiOption).
131-
- FunctionTaglessOption is parsed like [`TaglessOption`](###TaglessOption).
197+
- FunctionOption is parsed like [`ValueOption`](#ValueOption).
198+
- FunctionMultiOption is parsed like [`MultiOption`](#MultiOption).
199+
- FunctionTaglessOption is parsed like [`TaglessOption`](#TaglessOption).
200+
201+
### **Option Functions**
202+
203+
- Option
204+
- **exists()** -> option is present in the arguments list
205+
- **existsCount()** -> returns the number the option is present i.e -a -a -a will return 3
206+
- **value()** -> returns the raw argument value
207+
- **valueAs<T>** -> converts the raw argument to the given type
208+
- **valueBool()** -> returns true if argument is "1" or "True" or "true" or "t" or "y"
209+
- **execute()** -> only available with [`Function Options`](#FunctionOption/FunctionMultiOption/FunctionTaglessOption)
210+
- **values()** / **valuesAs<T>()** / **valuesBool()** -> same as their value counterpart but returns all provided values
211+
212+
## Customizations
213+
214+
### Asking For User Input When Mandatory Options Are Not Provided
215+
216+
- Call **userInputRequired()** in your program, user will be asked to input values for your mandatory values.
217+
- For inputs, input stream could be changed to your choosen stream via **changeIO()**
218+
219+
```c++
220+
Cli po(argc, argv);
221+
po.userInputRequired();
222+
```
223+
224+
### Making Invalid/Expanded Arguments Acceptable
225+
226+
- For example; when using tagless options, only 3 arguments are needed but calling the program with 4 arguments will cause to exit due to the unexpected argument.
227+
- This behaviour could be used to accept expanded files provided to the program such as "myprogram -a *.txt"
228+
- When used with other options, valid options will be parsed and program won't exit.
229+
230+
```c++
231+
Cli po(argc, argv);
232+
po.add(3);
233+
po.unexpectedArgumentsAcceptable();
234+
```
235+
236+
### Disabling Auto Help
237+
238+
- You can simply disable "-h" option by defining the value below before the header definition.
239+
240+
```c++
241+
#define BazPO_DISABLE_AUTO_HELP_MESSAGE
242+
```

0 commit comments

Comments
 (0)