CLI2: aliasExpansion & argv[0] handling

- Added ::handleArg0 method to clean up ::analyze.
- Implemented ::aliasExpansion, which now applies to lexemes, not full command
  line arguments.
This commit is contained in:
Paul Beckingham 2015-06-14 09:28:20 -04:00
parent de23fc3972
commit d43ca96056
2 changed files with 26 additions and 25 deletions

View file

@ -41,10 +41,10 @@ extern Context context;
// Overridden by rc.abbreviation.minimum. // Overridden by rc.abbreviation.minimum.
int CLI2::minimumMatchLength = 3; int CLI2::minimumMatchLength = 3;
/*
// Alias expansion limit. Any more indicates some kind of error. // Alias expansion limit. Any more indicates some kind of error.
static int safetyValveDefault = 10; static int safetyValveDefault = 10;
/*
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
A2::A2 () A2::A2 ()
: _name ("") : _name ("")
@ -351,20 +351,10 @@ void CLI2::add (const std::string& argument)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Intended to be called after ::add() to perform the final analysis. void CLI2::handleArg0 ()
void CLI2::analyze ()
{ {
// Start from scratch. // Capture arg0 separately, because it is the command that was run, and could
_args.clear (); // need special handling.
if (context.config.getInteger ("debug.parser") >= 3)
{
context.debug ("---------------------------------------------------------------------------------");
context.debug (dump ("CLI2::analyze start"));
}
// Capture _original_args[0] separately, because it is the command run, and
// could need special handling.
std::string raw = _original_args[0]; std::string raw = _original_args[0];
A2 a ("arg", raw, Lexer::Type::word); A2 a ("arg", raw, Lexer::Type::word);
a.tag ("ORIGINAL"); a.tag ("ORIGINAL");
@ -388,19 +378,29 @@ void CLI2::analyze ()
A2 cal ("argCal", "calendar", Lexer::Type::word); A2 cal ("argCal", "calendar", Lexer::Type::word);
_args.push_back (cal); _args.push_back (cal);
} }
}
////////////////////////////////////////////////////////////////////////////////
// Intended to be called after ::add() to perform the final analysis.
void CLI2::analyze ()
{
if (context.config.getInteger ("debug.parser") >= 3)
{
context.debug ("---------------------------------------------------------------------------------");
context.debug (dump ("CLI2::analyze start"));
}
// Start from scratch.
_args.clear ();
handleArg0 ();
// Look at each arg, and decide if it warrants lexing. // Look at each arg, and decide if it warrants lexing.
// Note: Starts interating at index 1. // Note: Starts interating at index 1.
for (unsigned int i = 1; i < _original_args.size (); ++i) for (unsigned int i = 1; i < _original_args.size (); ++i)
{ {
raw = _original_args[i];
// Lex each remaining argument. The apply a series of disqualifying tests
// that cause the lexemes to be ignored, and the original arugment used
// intact.
std::string lexeme; std::string lexeme;
Lexer::Type type; Lexer::Type type;
Lexer lex (raw); Lexer lex (_original_args[i]);
lex.ambiguity (false); lex.ambiguity (false);
while (lex.token (lexeme, type)) while (lex.token (lexeme, type))
@ -408,6 +408,7 @@ void CLI2::analyze ()
} }
// Now process _args. // Now process _args.
aliasExpansion ();
findOverrides (); findOverrides ();
if (context.config.getInteger ("debug.parser") >= 3) if (context.config.getInteger ("debug.parser") >= 3)
@ -822,7 +823,7 @@ void CLI2::addArg (const std::string& arg)
} }
} }
} }
*/
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void CLI2::aliasExpansion () void CLI2::aliasExpansion ()
{ {
@ -832,7 +833,7 @@ void CLI2::aliasExpansion ()
do do
{ {
action = false; action = false;
std::vector <A> reconstructed; std::vector <A2> reconstructed;
bool terminated = false; bool terminated = false;
std::string raw; std::string raw;
@ -849,7 +850,7 @@ void CLI2::aliasExpansion ()
auto lexed = Lexer::split (_aliases[raw]); auto lexed = Lexer::split (_aliases[raw]);
for (auto& l : lexed) for (auto& l : lexed)
{ {
A a ("argLex", l); A2 a ("argLex", l, Lexer::Type::word);
a.tag ("ALIAS"); a.tag ("ALIAS");
a.tag ("LEX"); a.tag ("LEX");
reconstructed.push_back (a); reconstructed.push_back (a);
@ -876,7 +877,6 @@ void CLI2::aliasExpansion ()
context.config.getInteger ("debug.parser") >= 3) context.config.getInteger ("debug.parser") >= 3)
context.debug (dump ("CLI2::analyze aliasExpansion")); context.debug (dump ("CLI2::analyze aliasExpansion"));
} }
*/
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void CLI2::findOverrides () void CLI2::findOverrides ()

View file

@ -110,8 +110,9 @@ public:
private: private:
/* /*
void addArg (const std::string&); void addArg (const std::string&);
void aliasExpansion ();
*/ */
void handleArg0 ();
void aliasExpansion ();
void findOverrides (); void findOverrides ();
/* /*
void categorize (); void categorize ();