Skip to content

Commit

Permalink
generic.cc: Fix a bug with mixed positional and optional arguments (#45
Browse files Browse the repository at this point in the history
…).
  • Loading branch information
sadjad committed Jul 29, 2020
1 parent 5518fb1 commit 1b830f5
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/models/generic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void generate_thunk( const CLIDescription & cli_description,
const int argc, char * argv[] )
{
vector<struct option> long_options;
string optstring;
string optstring = "-";

for ( const CLIOption & cli_option : cli_description.options() ) {
if ( cli_option.long_opt.initialized() ) {
Expand All @@ -50,11 +50,17 @@ void generate_thunk( const CLIDescription & cli_description,
roost::rbasename( cli_description.target_bin() ).string() );
vector<ThunkFactory::Data> indata;
vector<ThunkFactory::Output> outfiles;
vector<size_t> pos_args;

int opt;
while ( ( opt = getopt_long( argc, argv, optstring.c_str(), long_options.data(), NULL ) ) != -1 ) {
if ( opt == '?' ) continue;

if ( opt == 1 ) {
pos_args.push_back( optind - 1 );
continue;
}

for ( const CLIOption & option : cli_description.options() ) {
if ( option.value == opt ) {
if ( option.type == CLIOption::Type::OutFile ) {
Expand All @@ -70,13 +76,21 @@ void generate_thunk( const CLIDescription & cli_description,
}

for ( const size_t idx : cli_description.infile_args() ) {
indata.emplace_back( "", argv[ optind + idx ] );
if ( idx >= pos_args.size() ) {
throw runtime_error( "missing positional argument" );
}

indata.emplace_back( "", argv[ pos_args[ idx ] ] );
ThunkFactory::Data & this_indata = indata.back();
args[ ( optind + idx ) ] = thunk::data_placeholder( this_indata.hash() );
args[ pos_args[ idx ] ] = thunk::data_placeholder( this_indata.hash() );
}

for ( const size_t idx : cli_description.outfile_args() ) {
outfiles.emplace_back( argv[ optind + idx ] );
if ( idx >= pos_args.size() ) {
throw runtime_error( "missing positional argument" );
}

outfiles.emplace_back( args[ pos_args[ idx ] ] );
}

if ( outfiles.size() == 0 ) {
Expand Down

0 comments on commit 1b830f5

Please sign in to comment.