Looking for any good(and free) cross-platform CLI parsing library for a command line utility [closed]
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this questionI am currently working on a CLI utility which I feel can be improved a lot by adding a framework.
Basically the utility revolves around lots of command line syntax AND parsing the output. Currently the code is scattered all around with lots of individual inhouse run() and readFromRunOutput() calls with h开发者_如何学Cardcoded CLI arguments.
For example:
op=run("command -args");
while(readFromRunOutPUr(op))
{
// Process and take result from output
if(op=="result-one")
dothis();
else if(op=="result-two")
dothat();
}
I would like to create a generic framework out of this wherein the the following are fed in from outside the code through a XML file. 1) Test Name 2) Command Line invocation 3) Required Output for Success
So assumming a simple XML invocation...
<Test name=FirstTest>
<CLI="command -args">
<Success Output= "value" >
</Success>
</CLI>
</Test>
Yes. There are some few chinks to worry about in the above example but I think I have given the gist of it. Basically I would like to move all the different CLI invocations to outside the code and then feed them in(with the criteria for success and failure). For lot of other reasons I cannot go for any scripting languages like perl or python and have to maintain the product in C/C++.
From what I gather the requirements are
Defined XML syntax
Parser for the above XML spec to memory
Get CLI -----> and invoke
Read Output and compare with the fed in values
(Preferably cross platform support)
An implementation like this would not only standardize on all the different individual function calls but also make it easy to extend without any code changes.
Now my question is , are there any readymade free libraries(for C++) available which already provide similar or basic template framework on which I can extend. Preferably first hand experience. Any other guidance ?
Ok, to your real problem:
First you will need a proper XML parser (having used only Qt's XML and XERXES which is probably oversized for your use case i can't really recommend one, but libxml2 seems to be widely used) for your input.
If I understand you correctly you just pass the content of the CLI
tag to the command line, so no parsing is needed. Your real problem seems to be the Success Output
, right?
If so the first question you have to answer is what does that value mean? Does it suffice if the string is somewhere in the output? Is the context relevant? If so how can this be encoded? Regular expressions? Or rather different nodes in your XML config like <contains>
, ...
Depending on the answers you need to find different parsers. You most probably what to use a regex engine (for Example Boost::regex).
All this said, it looks like this is supposed to be some kind of testing framework, have you looked at the available alternatives, CMake's CTest comes to mind, there are probably lots of others out there.
I don't understand what exactly you are looking for, but have a look at Boost Program Options. It can handle most CL Parsing out of the box and is easily extendible for the rest.
Regarding my exact need..maybe a use case will help... Lets say my tool needs to check the output of "dir" for "file.txt". I need to write a function to run the command+parse the output+search for the input value. Now lets say there is another need to check the output of "dir" but this time check for value of "Volume serial number". Similar need but not same. Same for "dir" this time get the number of files as output. Now I can sit and write and cover all the scenrios. Or reuse an already existing library dedicated to different types of command line parsing.
Second use case I am hoping for is to hand over the finished product to other users(basically make it reusable). So that if an user has a test case which I have not covered, He can add it through a published schema ("Dir" ..get the directory of value).
So basically I am looking for a C/C++ friendly library dedicated to CLI parsing/processing with a flexible input framework. Alternative is to start doing that from scratch myself.
I use Glib for this. They have excelent methods for this.
// Declare some file-scope variables
static gint repeats = 2;
static gint max_size = 8;
static gboolean verbose = FALSE;
static gboolean beep = FALSE;
static gboolean rand = FALSE;
// Here you define the options you want
static GOptionEntry entries[] =
{
{ "repeats", 'r', 0, G_OPTION_ARG_INT, &repeats, "Average over N repetitions", "N" },
{ "max-size", 'm', 0, G_OPTION_ARG_INT, &max_size, "Test up to 2^M items", "M" },
{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL },
{ "beep", 'b', 0, G_OPTION_ARG_NONE, &beep, "Beep when done", NULL },
{ "rand", 0, 0, G_OPTION_ARG_NONE, &rand, "Randomize the data", NULL },
{ NULL }
};
int main (int argc, char *argv[]){
// You need this for correctly initiate the parser
GError *error = NULL;
GOptionContext *context;
context = g_option_context_new ("- test tree model performance");
g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
g_option_context_add_group (context, gtk_get_option_group (TRUE));
if (!g_option_context_parse (context, &argc, &argv, &error))
{
g_print ("option parsing failed: %s\n", error->message);
exit (1);
}
// Then comes your code
/* ... */
}
Read more at http://developer.gnome.org/glib/2.28/glib-Commandline-option-parser.html.
精彩评论