Builder tool - Command line arguments

This commit is contained in:
Emmanuel BENOîT 2017-12-02 14:59:20 +01:00
parent 3986bd5386
commit 10361fec4f

View file

@ -1,8 +1,12 @@
#include "externals.hh"
#include "c-opast.hh"
#include "c-ops.hh"
#include "c-opcomp.hh"
#include "c-opopt.hh"
#include <getopt.h>
#include <ebcl/Files.hh>
#include <ebcl/SRDText.hh>
#include <ebcl/Algorithms.hh>
@ -12,6 +16,13 @@ using namespace opast;
namespace {
const struct option CmdLineOpts_[] = {
{ "log-level" , required_argument , 0 , 'L' } ,
{ "source-path" , required_argument , 0 , 's' } ,
{ "config" , required_argument , 0 , 'c' } ,
{ 0 , 0 , 0 , 0 }
};
/*============================================================================*/
@ -43,7 +54,34 @@ void WriteSRDError(
int main( int argc , char** argv )
{
uint32_t logLevel{ 0 };
// Parse command line
T_Optional< uint32_t > oLogLevel;
T_String oSrcPath;
int c;
while ( ( c = getopt_long( argc , argv , "L:s:c:" , CmdLineOpts_ , nullptr ) ) != -1 ) {
switch ( c ) {
case 'L':
oLogLevel = uint32_t( atoi( optarg ) );
break;
case 's':
if ( oSrcPath ) {
fprintf( stderr , "Duplicate source path\n" );
exit( EXIT_FAILURE );
}
oSrcPath = optarg;
break;
case 'c':
fprintf( stderr , "-c/--config option ignored for now\n" );
break;
}
}
// Logger setup
const uint32_t logLevel{ oLogLevel ? *oLogLevel : 0 };
const auto logger{ [=]( F_OPGenLog func , uint32_t level ) {
if ( level > logLevel ) {
return;
@ -54,7 +92,17 @@ int main( int argc , char** argv )
} };
// Open file
const T_String inputName( argc >= 2 ? argv[ 1 ] : "demo.srd" );
const T_String inputName( [&]() {
T_StringBuilder sb;
if ( oSrcPath ) {
sb << oSrcPath;
if ( !oSrcPath.endsWith( "/" ) ) {
sb << '/';
}
}
sb << "demo.srd";
return T_String{ std::move( sb ) };
}( ) );
T_File input( inputName , E_FileMode::READ_ONLY );
try {
input.open( );