summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cli/options.cxx36
-rw-r--r--cli/options.hxx25
2 files changed, 55 insertions, 6 deletions
diff --git a/cli/options.cxx b/cli/options.cxx
index 7edc7f2..1e782e3 100644
--- a/cli/options.cxx
+++ b/cli/options.cxx
@@ -214,6 +214,7 @@ namespace cli
// argv_file_scanner
//
int argv_file_scanner::zero_argc_ = 0;
+ std::string argv_file_scanner::empty_string_;
bool argv_file_scanner::
more ()
@@ -287,7 +288,25 @@ namespace cli
if (!more ())
throw eos_reached ();
- return args_.empty () ? base::peek () : args_.front ().c_str ();
+ return args_.empty () ? base::peek () : args_.front ().value.c_str ();
+ }
+
+ const std::string& argv_file_scanner::
+ peek_file ()
+ {
+ if (!more ())
+ throw eos_reached ();
+
+ return args_.empty () ? empty_string_ : *args_.front ().file;
+ }
+
+ std::size_t argv_file_scanner::
+ peek_line ()
+ {
+ if (!more ())
+ throw eos_reached ();
+
+ return args_.empty () ? 0 : args_.front ().line;
}
const char* argv_file_scanner::
@@ -300,7 +319,7 @@ namespace cli
return base::next ();
else
{
- hold_[i_ == 0 ? ++i_ : --i_].swap (args_.front ());
+ hold_[i_ == 0 ? ++i_ : --i_].swap (args_.front ().value);
args_.pop_front ();
return hold_[i_].c_str ();
}
@@ -338,7 +357,12 @@ namespace cli
if (!is.is_open ())
throw file_io_failure (file);
- while (!is.eof ())
+ files_.push_back (file);
+
+ arg a;
+ a.file = &*files_.rbegin ();
+
+ for (a.line = 1; !is.eof (); ++a.line)
{
string line;
getline (is, line);
@@ -445,10 +469,12 @@ namespace cli
continue;
}
- args_.push_back (s1);
+ a.value = s1;
+ args_.push_back (a);
}
- args_.push_back (s2);
+ a.value = s2;
+ args_.push_back (a);
}
}
diff --git a/cli/options.hxx b/cli/options.hxx
index a865148..6533432 100644
--- a/cli/options.hxx
+++ b/cli/options.hxx
@@ -12,6 +12,7 @@
//
// End prologue.
+#include <list>
#include <deque>
#include <iosfwd>
#include <string>
@@ -339,6 +340,19 @@ namespace cli
virtual void
skip ();
+ // Return the file path if the peeked at argument came from a file and
+ // the empty string otherwise. The reference is guaranteed to be valid
+ // till the end of the scanner lifetime.
+ //
+ const std::string&
+ peek_file ();
+
+ // Return the 1-based line number if the peeked at argument came from
+ // a file and zero otherwise.
+ //
+ std::size_t
+ peek_line ();
+
private:
const option_info*
find (const char*) const;
@@ -353,7 +367,15 @@ namespace cli
const option_info* options_;
std::size_t options_count_;
- std::deque<std::string> args_;
+ struct arg
+ {
+ std::string value;
+ const std::string* file;
+ std::size_t line;
+ };
+
+ std::deque<arg> args_;
+ std::list<std::string> files_;
// Circular buffer of two arguments.
//
@@ -363,6 +385,7 @@ namespace cli
bool skip_;
static int zero_argc_;
+ static std::string empty_string_;
};
template <typename X>