remove blt::logging from argparse

use std::cout now as it produces more logical outputs
v1
Brett 2023-08-07 22:39:25 -04:00
parent 2a656a687e
commit 4ae730c9bb
4 changed files with 48 additions and 24 deletions

View File

@ -280,7 +280,7 @@ namespace blt
static std::string filename(const std::string& path); static std::string filename(const std::string& path);
// expects that the current flag has already been consumed (advanced past), leaves tokenizer in a state where the next element is 'current' // expects that the current flag has already been consumed (advanced past), leaves tokenizer in a state where the next element is 'current'
static bool consumeArguments(arg_tokenizer& tokenizer, const arg_properties_t& properties, std::vector<arg_data_internal_t>& v_out); bool consumeArguments(arg_tokenizer& tokenizer, const std::string& flag, const arg_properties_t& properties, std::vector<arg_data_internal_t>& v_out) const;
void handlePositionalArgument(arg_tokenizer& tokenizer, size_t& last_pos); void handlePositionalArgument(arg_tokenizer& tokenizer, size_t& last_pos);
@ -314,7 +314,8 @@ namespace blt
arg_results parse_args(const std::vector<std::string>& args); arg_results parse_args(const std::vector<std::string>& args);
void printHelp(); void printUsage() const;
void printHelp() const;
~arg_parse() ~arg_parse()
{ {

View File

@ -12,7 +12,6 @@
#include <sstream> #include <sstream>
#include <algorithm> #include <algorithm>
#include <vector> #include <vector>
#include <blt/std/logging.h>
namespace blt::string { namespace blt::string {

View File

@ -4,7 +4,7 @@
* See LICENSE file for license detail * See LICENSE file for license detail
*/ */
#include <blt/parse/argparse.h> #include <blt/parse/argparse.h>
#include <blt/std/logging.h> #include <iostream>
#include <blt/std/string.h> #include <blt/std/string.h>
namespace blt namespace blt
@ -51,14 +51,17 @@ namespace blt
throw invalid_argument_exception("Flag '" + flag + "' must start with - or --"); throw invalid_argument_exception("Flag '" + flag + "' must start with - or --");
} }
arg_vector_t::arg_vector_t(const char* str) { arg_vector_t::arg_vector_t(const char* str)
{
std::string as_string(str); std::string as_string(str);
if (as_string.starts_with('-')) if (as_string.starts_with('-'))
flags.emplace_back(as_string); flags.emplace_back(as_string);
else else
name = as_string; name = as_string;
} }
arg_vector_t::arg_vector_t(const std::string& str) {
arg_vector_t::arg_vector_t(const std::string& str)
{
if (str.starts_with('-')) if (str.starts_with('-'))
flags.emplace_back(str); flags.emplace_back(str);
else else
@ -89,9 +92,11 @@ namespace blt
std::string to_string(const arg_data_internal_t& v) std::string to_string(const arg_data_internal_t& v)
{ {
if (std::holds_alternative<std::string>(v)){ if (std::holds_alternative<std::string>(v))
{
return std::get<std::string>(v); return std::get<std::string>(v);
} else if (std::holds_alternative<bool>(v)) { } else if (std::holds_alternative<bool>(v))
{
return std::get<bool>(v) ? "True" : "False"; return std::get<bool>(v) ? "True" : "False";
} }
return std::to_string(std::get<int32_t>(v)); return std::to_string(std::get<int32_t>(v));
@ -146,7 +151,9 @@ namespace blt
user_args.arg_properties_storage.push_back(properties); user_args.arg_properties_storage.push_back(properties);
} }
bool arg_parse::consumeArguments(arg_tokenizer& tokenizer, const arg_properties_t& properties, std::vector<arg_data_internal_t>& v_out) bool arg_parse::consumeArguments(
arg_tokenizer& tokenizer, const std::string& flag, const arg_properties_t& properties, std::vector<arg_data_internal_t>& v_out
) const
{ {
switch (properties.a_nargs.flags) switch (properties.a_nargs.flags)
{ {
@ -156,13 +163,19 @@ namespace blt
// if we don't have another arg to consume we have a problem! // if we don't have another arg to consume we have a problem!
if (!tokenizer.hasCurrent()) if (!tokenizer.hasCurrent())
{ {
BLT_WARN("Expected %d arguments got %d instead!", properties.a_nargs.args, i); // TODO: S
printUsage();
std::cout << filename(loaded_args.program_name) << ": error: flag '" << flag << "' expected " << properties.a_nargs.args
<< " argument(s) got " << i << " argument(s) instead!\n";
return false; return false;
} }
// if we do have one, but it is a flag then we also have a problem! // if we do have one, but it is a flag then we also have a problem!
if (tokenizer.isFlag()) if (tokenizer.isFlag())
{ {
BLT_WARN("Expected %d arguments, found flag instead!", properties.a_nargs.args); printUsage();
std::cout << filename(loaded_args.program_name) << ": error: flag '" << flag << "' expected " << properties.a_nargs.args
<< " argument(s) but found '" << tokenizer.get() << "' instead!\n";
//BLT_WARN("Expected %d arguments, found flag instead!", properties.a_nargs.args);
return false; return false;
} }
// get the value and advance // get the value and advance
@ -194,7 +207,8 @@ namespace blt
case arg_nargs_t::ALL_REQUIRED: case arg_nargs_t::ALL_REQUIRED:
if (tokenizer.hasCurrent() && tokenizer.isFlag()) if (tokenizer.hasCurrent() && tokenizer.isFlag())
{ {
BLT_WARN("At least one argument is required!"); printUsage();
std::cout << loaded_args.program_name << ": at least one argument is required for '" << flag << "'\n";
return false; return false;
} }
while (tokenizer.hasCurrent() && !tokenizer.isFlag()) while (tokenizer.hasCurrent() && !tokenizer.isFlag())
@ -232,14 +246,15 @@ namespace blt
{ {
// handle special args like -vvv // handle special args like -vvv
if (!flag.starts_with('-')) if (!flag.starts_with('-'))
BLT_ERROR("Flag processed but does not start with '-'"); std::cerr << "Flag processed but does not start with '-' (this is a serious error!)\n";
// make sure the flag only contains the same character // make sure the flag only contains the same character
auto type = flag[1]; auto type = flag[1];
for (char c : flag.substr(1)) for (char c : flag.substr(1))
{ {
if (c != type) if (c != type)
{ {
BLT_ERROR("Processed flag '%s' expected %c found %c", flag.c_str(), type, c); printUsage();
std::cout << "found different characters in flag '" << flag.c_str() << "' expected '" << type << "' but found '" << c << "'\n";
return; return;
} }
} }
@ -282,12 +297,14 @@ namespace blt
{ {
arg_data_t& data = loaded_args.data[dest]; arg_data_t& data = loaded_args.data[dest];
arg_data_vec_t v; arg_data_vec_t v;
if (!consumeArguments(tokenizer, *properties, v)) if (!consumeArguments(tokenizer, flag, *properties, v))
{ {
printHelp(); printHelp();
return; return;
} }
if (v.size() == 1) if (v.empty())
data = "";
else if (v.size() == 1)
data = v[0]; data = v[0];
else else
data = v; data = v;
@ -318,7 +335,7 @@ namespace blt
case arg_action_t::VERSION: case arg_action_t::VERSION:
{ {
auto file = filename(loaded_args.program_name); auto file = filename(loaded_args.program_name);
BLT_INFO("%s, %s", file.c_str(), properties->a_version.c_str()); std::cout << file.c_str() << " " << properties->a_version << "\n";
break; break;
} }
case arg_action_t::APPEND_CONST: case arg_action_t::APPEND_CONST:
@ -338,7 +355,7 @@ namespace blt
if (!holds_alternative<arg_data_vec_t>(data)) if (!holds_alternative<arg_data_vec_t>(data))
data = arg_data_vec_t(); data = arg_data_vec_t();
auto& l = get<arg_data_vec_t>(data); auto& l = get<arg_data_vec_t>(data);
consumeArguments(tokenizer, *properties, l); consumeArguments(tokenizer, flag, *properties, l);
break; break;
} }
} }
@ -369,7 +386,8 @@ namespace blt
} }
// load defaults for args which were not found // load defaults for args which were not found
for (const auto* arg : user_args.arg_properties_storage) { for (const auto* arg : user_args.arg_properties_storage)
{
if (!to_string(arg->a_default).empty() && !loaded_args.contains(arg->a_dest)) if (!to_string(arg->a_default).empty() && !loaded_args.contains(arg->a_dest))
loaded_args.data[arg->a_dest] = arg->a_default; loaded_args.data[arg->a_dest] = arg->a_default;
} }
@ -389,16 +407,22 @@ namespace blt
} }
// remove the last space caused by the for loop // remove the last space caused by the for loop
unrec = unrec.substr(0, unrec.size() - 1); unrec = unrec.substr(0, unrec.size() - 1);
// TODO: use exceptions? printUsage();
BLT_WARN("Unrecognized args: %s", unrec.c_str()); std::cout << loaded_args.program_name << ": error: unrecognized args: " << unrec << "\n";
printHelp(); printHelp();
return loaded_args; return loaded_args;
} }
void arg_parse::printHelp() void arg_parse::printHelp() const
{ {
BLT_TRACE("I am helpful!"); std::cout << ("I am helpful!\n");
std::exit(0); std::exit(0);
} }
void arg_parse::printUsage() const
{
}
} }

View File

@ -70,7 +70,7 @@ int (*func_func_in)(int) = &test_as_func;
int main(int argc, const char** argv) { int main(int argc, const char** argv) {
blt::arg_parse parser; blt::arg_parse parser;
parser.addArgument(blt::arg_builder({"--poo", "-p"}).build()); parser.addArgument(blt::arg_builder({"--poo", "-p"}).build());
parser.addArgument(blt::arg_builder({"--foo", "-f"}).setAction(blt::arg_action_t::STORE_TRUE).setDefault(false).build()); parser.addArgument(blt::arg_builder("--foo").setAction(blt::arg_action_t::STORE_TRUE).setDefault(false).build());
parser.addArgument(blt::arg_builder({"--goo", "-g"}).build()); parser.addArgument(blt::arg_builder({"--goo", "-g"}).build());
parser.addArgument(blt::arg_builder({"--oop", "-o"}).build()); parser.addArgument(blt::arg_builder({"--oop", "-o"}).build());
parser.addArgument(blt::arg_builder("Sexy_pos").build()); parser.addArgument(blt::arg_builder("Sexy_pos").build());