views:

41

answers:

3

I 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 hardcoded 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 ?

A: 

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.

Fabio Fracassi
Fabio...Glanced through the link..Looks bit too complex for my current needs. Thanks anyway!
Now that you explained more on what you really want to do its not the right Tool anyway, It does CL argument parsing, which has nothing to do with what you are trying.
Fabio Fracassi
Fabio ... I appreciate your help very much. The requirements are very fluid at this point and I have some bunch of current requirements and hence could not respond faster...Hopefully this would get clearer in the next couple of months and I can come back with more clarity. Thanks again.
A: 

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.

Your last paragraph is missleading, what you want is not CLI parsing, this term is commonly used to describe the parsing of command line arguments, i.e. interpreting a line like `du -h --max-depth=1` and then being able to do something like `if (cli["human-readable"]) { /* change output*/ }` inside the du executable.What you want to do is to parse the output of the CL tools, which is generally just generic text parsing.I know of no tool that does that out of the box, but look at Unit-test or continuous integration framework, they will probably have something similar integrated
Fabio Fracassi
+1  A: 

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.

Fabio Fracassi