Skip to content

Commit d9b2dd7

Browse files
committed
Added Cli class
1 parent c5a366c commit d9b2dd7

File tree

4 files changed

+226
-1
lines changed

4 files changed

+226
-1
lines changed

doxyfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ PROJECT_NAME = StratifyLib
3838
# could be handy for archiving the generated documentation or if some version
3939
# control system is used.
4040

41-
PROJECT_NUMBER = 2.0.0
41+
PROJECT_NUMBER = 2.1.0
4242

4343
# Using the PROJECT_BRIEF tag one can provide an optional one line description
4444
# for a project that appears at the top of each page and should give viewer a

include/sys.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace sys {};
2020
#include "sys/Dbug.hpp"
2121
#include "sys/DbugStdout.hpp"
2222
#include "sys/DbugUart.hpp"
23+
#include "sys/Cli.hpp"
2324
#endif
2425

2526
#include "sys/Trace.hpp"

include/sys/Cli.hpp

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*! \file */ //Copyright 2011-2016 Tyler Gilbert; All Rights Reserved
2+
3+
#ifndef SYS_CLI_HPP_
4+
#define SYS_CLI_HPP_
5+
6+
#include "../var/String.hpp"
7+
#include "../var/Token.hpp"
8+
9+
namespace sys {
10+
11+
12+
/*! \brief Command Line Interface class
13+
* \details This class contains methods to help analyze input from the
14+
* command line.
15+
*/
16+
class Cli {
17+
public:
18+
19+
/*! \details Construct a new object to parse and handle command line interface arguments */
20+
Cli(int argc, char * argv[]);
21+
22+
/*! \details Set the version of the program */
23+
void set_version(const char * version){ m_version = version; }
24+
25+
/*! \details Set the publisher of the program */
26+
void set_publisher(const char * publisher){ m_publisher = publisher; }
27+
28+
/*! \details Print the version, name and publisher on the stdout */
29+
void print_version();
30+
31+
/*! \details Access the program version */
32+
const char * version() const { return m_version; }
33+
34+
/*! \details Access the program publisher */
35+
const char * publisher() const { return m_publisher; }
36+
37+
/*! \details Access the program name */
38+
const char * name() const { return m_name; }
39+
40+
/*! \details Return the argument offset by value as a var::String */
41+
var::String at(u16 value);
42+
43+
/*! \details Return the argument offset by value as a pio_t value */
44+
pio_t pio_at(u16 value);
45+
46+
/*! \details Return the argument offset by value as an int value */
47+
int value_at(u16 value);
48+
49+
/*! \details This method returns true if \a value is one of the options
50+
*
51+
* @param value A pointer to the option string to search for
52+
* @return True if value is any of the options available
53+
*
54+
* For example,
55+
*
56+
* program --version -i text.txt
57+
*
58+
* is_option("--version") will return true.
59+
*
60+
*/
61+
bool is_option(const char * value);
62+
63+
/*! \details Get the argument of an option as a var::String
64+
*
65+
* @param option The option to match
66+
* @return A String containing the option argument
67+
*
68+
* For example, take the given command line
69+
* program -i filename.txt
70+
*
71+
* get_option_argument("-i") will return a String containing "filename.txt"
72+
*
73+
*
74+
*/
75+
var::String get_option_argument(const char * option);
76+
77+
/*! \details Get the argument of an option as a var::String
78+
*
79+
* @param option The option to match
80+
* @return The value of the argument or 0 if the option wasn't found
81+
*
82+
* For example, take the given command line
83+
* program -i 10
84+
*
85+
* get_option_argument("-i") will return 10
86+
*
87+
*
88+
*/
89+
int get_option_value(const char * option);
90+
91+
/*! \details Get the argument of an option as a var::String
92+
*
93+
* @param option The option to match
94+
* @return The value of the argument or 0 if the option wasn't found
95+
*
96+
* For example, take the given command line
97+
* program -i 2.1
98+
*
99+
* get_option_argument("-i") will return port = 2 and pin = 1
100+
*
101+
*
102+
*/
103+
pio_t get_option_pio(const char * option);
104+
105+
106+
int size() const { return m_argc; }
107+
108+
private:
109+
u16 m_argc;
110+
char ** m_argv;
111+
var::String m_version;
112+
var::String m_publisher;
113+
var::String m_name;
114+
115+
116+
};
117+
118+
} /* namespace sys */
119+
120+
#endif /* SYS_CLI_HPP_ */

src/sys/Cli.cpp

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*! \file */ //Copyright 2011-2016 Tyler Gilbert; All Rights Reserved
2+
3+
#include "sys/Cli.hpp"
4+
5+
using namespace var;
6+
7+
namespace sys {
8+
9+
Cli::Cli(int argc, char * argv[]){
10+
if( argc < 0 ){
11+
argc = 0;
12+
}
13+
m_argc = argc;
14+
m_argv = argv;
15+
16+
if( argc > 0 ){
17+
m_name = argv[0];
18+
}
19+
}
20+
21+
void Cli::print_version(){
22+
printf("%s version: %s by %s\n", m_name.c_str(), m_version.c_str(), m_publisher.c_str());
23+
}
24+
25+
String Cli::at(u16 value){
26+
String arg;
27+
if( value < m_argc ){
28+
arg.assign(m_argv[value]);
29+
}
30+
return arg;
31+
}
32+
33+
String Cli::get_option_argument(const char * option){
34+
u16 args;
35+
for(args = 0; args < m_argc; args++){
36+
if( at(args) == option ){
37+
return at(args+1);
38+
}
39+
}
40+
return String();
41+
}
42+
43+
bool Cli::is_option(const char * value){
44+
u16 i;
45+
for(i=0; i < m_argc; i++){
46+
if( at(i) == value ){
47+
return true;
48+
}
49+
}
50+
return false;
51+
}
52+
53+
int Cli::get_option_value(const char * option){
54+
String arg = get_option_argument(option);
55+
if( arg.empty() ){
56+
return 0;
57+
}
58+
return arg.atoi();
59+
}
60+
61+
pio_t Cli::get_option_pio(const char * option){
62+
pio_t pio;
63+
Token arg;
64+
arg.assign(get_option_argument(option).c_str());
65+
arg.parse(".");
66+
67+
if( arg.size() == 2 ){
68+
pio.port = atoi(arg.at(0));
69+
pio.pin = atoi(arg.at(1));
70+
} else {
71+
pio.port = 255;
72+
pio.pin = 255;
73+
}
74+
75+
return pio;
76+
}
77+
78+
pio_t Cli::pio_at(u16 value){
79+
pio_t pio;
80+
Token arg;
81+
82+
arg.assign( at(value).c_str() );
83+
arg.parse(".");
84+
85+
if( arg.size() == 2 ){
86+
pio.port = atoi(arg.at(0));
87+
pio.pin = atoi(arg.at(1));
88+
} else {
89+
pio.port = 255;
90+
pio.pin = 255;
91+
}
92+
93+
return pio;
94+
}
95+
96+
int Cli::value_at(u16 value){
97+
return at(value).atoi();
98+
}
99+
100+
101+
102+
103+
104+
} /* namespace sys */

0 commit comments

Comments
 (0)