Proper option parser for the Sketch test case

llvm-svn: 178477
This commit is contained in:
Enrico Granata 2013-04-01 17:42:02 +00:00
parent 474b9315ad
commit 4b4e24da61
1 changed files with 221 additions and 7 deletions

View File

@ -18,6 +18,7 @@
#include <iostream>
#include <unistd.h>
#include <fstream>
#include <getopt.h>
using namespace lldb_perf;
@ -190,6 +191,52 @@ public:
results.Write(m_out_path.c_str());
}
void
SetExecutablePath (const char* str)
{
if (str)
m_app_path.assign(str);
}
const char*
GetExecutablePath ()
{
if (m_app_path.empty())
return NULL;
return m_app_path.c_str();
}
void
SetDocumentPath (const char* str)
{
if (str)
m_doc_path.assign(str);
}
const char*
GetDocumentPath ()
{
if (m_doc_path.empty())
return NULL;
return m_doc_path.c_str();
}
void
SetResultFilePath (const char* str)
{
if (str)
m_out_path.assign(str);
}
const char*
GetResultFilePath ()
{
if (m_out_path.empty())
return NULL;
return m_out_path.c_str();
}
private:
Measurement<lldb_perf::TimeGauge, std::function<void()>> m_fetch_frames_measurement;
Measurement<lldb_perf::TimeGauge, std::function<void(const char*, uint32_t)>> m_file_line_bp_measurement;
@ -202,13 +249,180 @@ private:
std::string m_out_path;
};
// argv[1] == path to app
// argv[2] == path to document
// argv[3] == path to result
int main(int argc, const char * argv[])
struct Options
{
SketchTest skt;
TestCase::Run(skt,argc,argv);
return 0;
std::string sketch_path;
std::string foobar_path;
std::string out_file;
bool verbose;
bool error;
bool print_help;
Options() :
verbose (false),
error (false),
print_help (false)
{
}
};
static struct option g_long_options[] = {
{ "verbose", no_argument, NULL, 'v' },
{ "sketch", required_argument, NULL, 'c' },
{ "foobar", required_argument, NULL, 'f' },
{ "out-file", required_argument, NULL, 'o' },
{ NULL, 0, NULL, 0 }
};
std::string
GetShortOptionString (struct option *long_options)
{
std::string option_string;
for (int i = 0; long_options[i].name != NULL; ++i)
{
if (long_options[i].flag == NULL)
{
option_string.push_back ((char) long_options[i].val);
switch (long_options[i].has_arg)
{
default:
case no_argument:
break;
case required_argument:
option_string.push_back (':');
break;
case optional_argument:
option_string.append (2, ':');
break;
}
}
}
return option_string;
}
int main(int argc, const char * argv[])
{
// Prepare for & make calls to getopt_long.
SketchTest test;
std::string short_option_string (GetShortOptionString(g_long_options));
Options option_data;
bool done = false;
#if __GLIBC__
optind = 0;
#else
optreset = 1;
optind = 1;
#endif
while (!done)
{
int long_options_index = -1;
const int short_option = ::getopt_long_only (argc,
const_cast<char **>(argv),
short_option_string.c_str(),
g_long_options,
&long_options_index);
switch (short_option)
{
case 0:
// Already handled
break;
case -1:
done = true;
break;
case '?':
option_data.print_help = true;
break;
case 'h':
option_data.print_help = true;
break;
case 'v':
option_data.verbose = true;
break;
case 'c':
{
SBFileSpec file(optarg);
if (file.Exists())
test.SetExecutablePath(optarg);
else
fprintf(stderr, "error: file specified in --sketch (-c) option doesn't exist: '%s'\n", optarg);
}
break;
case 'f':
{
SBFileSpec file(optarg);
if (file.Exists())
test.SetDocumentPath(optarg);
else
fprintf(stderr, "error: file specified in --foobar (-f) option doesn't exist: '%s'\n", optarg);
}
break;
case 'o':
test.SetResultFilePath(optarg);
break;
default:
option_data.error = true;
option_data.print_help = true;
fprintf (stderr, "error: unrecognized option %c\n", short_option);
break;
}
}
if (test.GetExecutablePath() == NULL)
{
// --sketch is mandatory
option_data.print_help = true;
option_data.error = true;
fprintf (stderr, "error: the '--sketch=PATH' option is mandatory\n");
}
if (test.GetDocumentPath() == NULL)
{
// --foobar is mandatory
option_data.print_help = true;
option_data.error = true;
fprintf (stderr, "error: the '--foobar=PATH' option is mandatory\n");
}
if (option_data.print_help)
{
puts(R"(
NAME
lldb_perf_sketch -- a tool that measures LLDB peformance while debugging sketch.
SYNOPSIS
lldb_perf_sketch --sketch=PATH --foobar=PATH [--out-file=PATH --verbose]
DESCRIPTION
Runs a set of static timing and memory tasks against sketch and outputs results
to a plist file.
)");
}
if (option_data.error)
{
exit(1);
}
// Update argc and argv after parsing options
argc -= optind;
argv += optind;
test.SetVerbose(option_data.verbose);
TestCase::Run(test, argc, argv);
return 0;
}