|
|
@@ -11,23 +11,36 @@
|
|
|
#include <unistd.h>
|
|
|
#include "params.hh"
|
|
|
|
|
|
-Params::Params(int ac, char **av) :progName(*av), params(std::list<std::string>(ac -1)), strict(true)
|
|
|
+#include "config.h"
|
|
|
+
|
|
|
+Params::Params(int ac, char **av) :progName(*av), strict(true)
|
|
|
+{
|
|
|
+ av++;
|
|
|
+ while (*av)
|
|
|
+ {
|
|
|
+ params.push_back(*av);
|
|
|
+ av++;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+bool Params::read()
|
|
|
{
|
|
|
bool written = false;
|
|
|
std::stringstream *input = nullptr;
|
|
|
ignoreUnicode = false;
|
|
|
- colorMode = isatty(1);
|
|
|
+ colorMode = true;
|
|
|
|
|
|
- while (*(++av))
|
|
|
+ for (std::list<std::string>::const_iterator i = params.cbegin(); i != params.cend(); i++)
|
|
|
{
|
|
|
- std::string tmp(*av);
|
|
|
+ std::string tmp(*i);
|
|
|
if (!input)
|
|
|
{
|
|
|
if (tmp == "-f")
|
|
|
{
|
|
|
- tmp = *(++av);
|
|
|
- if (!*av)
|
|
|
+ i++;
|
|
|
+ if (i == params.cend())
|
|
|
throw std::runtime_error("Invalid use of -f without argument");
|
|
|
+ tmp = *i;
|
|
|
std::ifstream *in = new std::ifstream(tmp);
|
|
|
if (!in->is_open())
|
|
|
{
|
|
|
@@ -46,15 +59,23 @@ Params::Params(int ac, char **av) :progName(*av), params(std::list<std::string>(
|
|
|
ignoreUnicode = true;
|
|
|
else if (tmp == "--color")
|
|
|
colorMode = true;
|
|
|
+ else if (tmp == "--help" || tmp == "-h")
|
|
|
+ {
|
|
|
+ usage();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ else if (tmp == "--version" || tmp == "-v")
|
|
|
+ {
|
|
|
+ version();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
else if (tmp.find("--color=") == 0)
|
|
|
{
|
|
|
- std::string mode = (*av) + 8;
|
|
|
+ std::string mode = tmp.substr(8);
|
|
|
if (mode == "always")
|
|
|
colorMode = true;
|
|
|
else if (mode == "never")
|
|
|
colorMode = false;
|
|
|
- else if (mode == "auto")
|
|
|
- colorMode = isatty(1);
|
|
|
else
|
|
|
throw std::runtime_error("Invalid option for --color: " +mode);
|
|
|
}
|
|
|
@@ -72,6 +93,7 @@ Params::Params(int ac, char **av) :progName(*av), params(std::list<std::string>(
|
|
|
}
|
|
|
if (!this->input)
|
|
|
this->input = input;
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
Params::~Params()
|
|
|
@@ -87,17 +109,38 @@ std::basic_istream<char> &Params::getInput() const
|
|
|
return std::cin;
|
|
|
}
|
|
|
|
|
|
-void Params::usage(const std::string &progName) noexcept
|
|
|
+void Params::usage() const noexcept
|
|
|
{
|
|
|
- std::cout << "Usage: " << progName << " [OPTIONS] [--] INPUT" << std::endl;
|
|
|
- std::cout << "\t\t-f filename\tread input from (filename) instead of stdin" << std::endl;
|
|
|
- std::cout << "\t\t--ascii\tignore unicode values" << std::endl;
|
|
|
- std::cout << "\t\t--color[=MODE]\tColorize output, MODE can be always (default when ommited), never or auto (default if --color is ommited)" << std::endl;
|
|
|
- std::cout << "\t\t-W\tdisable strict mode (warning does not interrupt reading)" << std::endl;
|
|
|
+ std::cout << "Usage: " << progName << " [OPTIONS] [--] INPUT" << std::endl
|
|
|
+ << "Read json input and print it using ncurse" << std::endl << std::endl
|
|
|
+ << "if not INPUT nor -f, use standard input" << std::endl << std::endl
|
|
|
+
|
|
|
+ << " -f filename\t\tread input from filename instead of stdin" << std::endl
|
|
|
+ << " -W \t\t\tconsider continuing on non-blocking errors" << std::endl
|
|
|
+ << " --ascii\t\tignore unicode values" << std::endl
|
|
|
+ << " --color[=MODE]\tcolorize output, MODE can be never or always (default when ommited)" << std::endl
|
|
|
+ << " -v, -version\t\tdisplay version information" << std::endl
|
|
|
+ << " -h, --helph\t\tshow this message and exit" << std::endl << std::endl
|
|
|
+
|
|
|
+ << "Examples:" << std::endl
|
|
|
+ << STROLL_PROGNAME << " -f f.json\tOutput f.json's content" << std::endl << std::endl
|
|
|
+
|
|
|
+ << "Report bugs to <isundill@gmail.com>" << std::endl;
|
|
|
}
|
|
|
|
|
|
-bool Params::isValid() const
|
|
|
-{ return true; }
|
|
|
+void Params::version() const noexcept
|
|
|
+{
|
|
|
+ std::cout << STROLL_PROGNAME << " (jsonstroller suite) " << VERSION << " generated on " << VERSIONDATE << std::endl << std::endl
|
|
|
+
|
|
|
+ << "Copyright (C) 2016 Free Software Foundation, Inc." << std::endl
|
|
|
+ << "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>." << std::endl
|
|
|
+
|
|
|
+ << "This is free software: you are free to change and redistribute it." << std::endl
|
|
|
+ << "There is NO WARRANTY, to the extent permitted by law." << std::endl << std::endl
|
|
|
+
|
|
|
+ << "Written by isundil <isundill@gmail.com>." << std::endl;
|
|
|
+
|
|
|
+}
|
|
|
|
|
|
bool Params::isStrict() const
|
|
|
{ return strict; }
|