Skip to content
This repository has been archived by the owner on Nov 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from hurbIndustries/dev
Browse files Browse the repository at this point in the history
0.0.4 Rollout.
  • Loading branch information
Captainjamason authored Feb 11, 2024
2 parents abb7ff2 + f5a3c3a commit a67749d
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: C/C++ CI

on:
push:
branches: [ "main" ]
branches: [ "main", "dev" ]
pull_request:
branches: [ "main" ]

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*.out
*.app

# Custom
build
.cache/
*.core
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int main(int argc, char *argv[]) {
opencxx_cli::CLI cli;

std::vector<CLI::entryData> entries;
cli.addEntry("--test", "-t", testFunc, &entries);
cli.addEntry("--test", "-t", testFunc, &entries, false, "This will show on help()");
cli.parse(entries, cli.vectorize(argc, argv));

return 0;
Expand All @@ -28,13 +28,20 @@ int main(int argc, char *argv[]) {
More information can be found in the wiki. But the general idea is to create a vector to store entries, add entries, then run `cli.parse()` to let it handle the rest.
## Planned Features:
## Features:
- [x] Basic Color Manipulation.
- [ ] Automated argument handling.
- [ ] Populate `--help` with new argument checks.
- [X] Automated argument handling.
- [X] Populate `--help` with new argument checks.
- [x] Iterate over vector of arguments
- [X] Call respective functions that were declared earlier in code.
- [X] `returnArg()`, Used for getting the next argument for custom functions.
- [ ] Flavortext and colorizing of default commands.
- [ ] Options for default menu colorizations through a basic function and structs.
- [ ] `libCURL` like configuration on the fly.
- [ ] `--version` default menu available for fetching too.
__More coming soon__
## Compiling/Installing
### Requirements:
Expand All @@ -56,4 +63,6 @@ Use the provided `./install.sh` file, While it's currently a quick and dirty scr
Made with love. Started by JPD
<3
__Questions? Comments? Requests? Want to chat? Join our Discord:__ https://discord.gg/JCHa4h4Y55
hurbIndustries 2024
4 changes: 3 additions & 1 deletion include/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace opencxx_cli {
std::string shand;
int(*func)();
std::string desc;
bool arg;
};

// Basic commands.
Expand All @@ -38,12 +39,13 @@ namespace opencxx_cli {

// addEntry will reference the struct above, Used to add into a vector that
// is provided from the application.
int addEntry(std::string lhand, std::string shand, int (*func)(), std::vector<CLI::entryData> *entries, std::string desc = "Add a descrption in addEntry()");
int addEntry(std::string lhand, std::string shand, int (*func)(), std::vector<CLI::entryData> *entries, bool arg = false,std::string desc = "Add a descrption in addEntry()");

// Useful function to quickly make an argument vector, this can be run
// inline.
std::vector<std::string> vectorize(int argc, char *argv[]);
int parse(std::vector<entryData> entries, std::vector<std::string> args);
std::string returnArg();

// CLI output messages.
int error(std::string s);
Expand Down
38 changes: 36 additions & 2 deletions src/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
#include "../include/cli.h"
#include "../include/colors.h"

// Namespace declarations here, May rid of in the future.
using namespace opencxx_cli;
using namespace std;

// Global argument return string. I wish this was handled a bit better but for now its good.
string argReturn;

// Initialize the main 3 debug/error message commands,
// along with some nice formatting from the color library.
int CLI::error(string s) {
Expand Down Expand Up @@ -41,14 +45,27 @@ int CLI::info(string s) {
return 0;
}

// Small helper function to set the argReturn varaible.
int addArg(string arg) {
argReturn = arg;
return 1;
}

// returnArg() *must* be called from a user function if `arg = true`, This will be beneficial for
// any sort of functions that require parameters.
string CLI::returnArg() {
return argReturn;
}

// addEntry() will push a new entryData struct (cli.h) into an entries
// vector, which is established application side and a vector is provided.
int CLI::addEntry(string lhand, string shand, int(*func)(), vector<CLI::entryData> *entries, std::string desc) {
int CLI::addEntry(string lhand, string shand, int(*func)(), vector<CLI::entryData> *entries, bool arg, std::string desc) {
CLI::entryData entry;
entry.lhand = lhand;
entry.shand = shand;
entry.func = func;
entry.desc = desc;
entry.arg = arg;
entries -> push_back(entry);
if(CLI::debug == true) {
cout << entry.shand << "\n";
Expand Down Expand Up @@ -78,6 +95,7 @@ vector<string> CLI::vectorize(int argc, char *argv[]) {
return args;
}

// help() is bound to `--help` by default. May be customizable in the future.
int CLI::help(vector<CLI::entryData> entries) {
//cout << "[opencxx-cli] Help is under construction...\n";
CLI::programInfo pgInfo;
Expand Down Expand Up @@ -120,11 +138,27 @@ int CLI::parse(vector<CLI::entryData> entries, vector<string> args) {
cout << entries[j].lhand << "\n";
}
if(args[i] == entries[j].shand) {
if(entries[j].arg) {
if(args[i+1] != "") {
addArg(args[i+1]);
} else {
error("Please provide an argument...");
return 1;
}
}
entries[j].func();
return(0);
} else if (args[i] == entries[j].lhand) {
if(entries[j].arg) {
if(args[i+1] != "") {
addArg(args[i+1]);
} else {
error("Please provide an argument...");
return 1;
}
}
entries[j].func();
return(0);
return 0;
}
else if (args[i] != "--opencxx-cli-debug" && j == args.size()) {
error("Invalid arguments... Please use --help");
Expand Down
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ test('addEntryTwice', unitTests, args : ['-addEntryTwice'])
test('testParse1', unitTests, args : ['-testParse', '--test'])
test('testParse2', unitTests, args : ['-testParse', '--t2'])
test('help', unitTests, args : ['-help'])
test('testArgPass', unitTests, args : ['-testArgPass', '--test', ''])
pkg.generate(opencxx_cli)
22 changes: 19 additions & 3 deletions src/unitTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@
// Some basic initialization...
using namespace opencxx_cli;
std::vector<CLI::entryData> entries;
CLI cli;

int testFunc() {
return 0;
}

int testArgPassFunc() {
if(cli.returnArg() != "") {
return 0;
} else {
return 1;
}
}

int main(int argc, char* argv[]) {
CLI cli;
std::string test = argv[1];

// test output messages...
Expand All @@ -36,19 +44,21 @@ int main(int argc, char* argv[]) {
}
}

// Test adding one entry.
if(test == "-addEntryOnce") {
cli.addEntry("--test", "-t", testFunc, &entries);
entries[0].func();
}

//test add entry
// Test adding two entry.
if(test == "-addEntryTwice") {
cli.addEntry("--test", "-t", testFunc, &entries);
cli.addEntry("-test2", "-t2", testFunc, &entries);
entries[0].func();
entries[1].func();
}

// Test parsing of arguments.
if(test == "-testParse") {
cli.addEntry("--test", "-t", testFunc, &entries);
cli.addEntry("--t2", "-t2", testFunc, &entries);
Expand All @@ -62,4 +72,10 @@ int main(int argc, char* argv[]) {
return 1;
}
}
}

// test argument passing()
if(test == "-testArgPass") {
cli.addEntry("--test", "-t", testArgPassFunc, &entries, true);
cli.parse(entries, cli.vectorize(argc, argv));
}
}

0 comments on commit a67749d

Please sign in to comment.