fixed argp code, implemented --all
This commit is contained in:
1 parent
62542734b5
commit
c87dd241a0
3 files changed
+99
-58
No files matched your search
@@ -19,6 +19,7 @@ typedef struct {
|
|||||||
bool tree_view;
|
bool tree_view;
|
||||||
} arguments_t;
|
} arguments_t;
|
||||||
|
|
||||||
|
void initialise_args( arguments_t* arguments );
|
||||||
error_t parse_args( int argc, char** argv, arguments_t* arguments );
|
error_t parse_args( int argc, char** argv, arguments_t* arguments );
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+91
-44
@@ -1,67 +1,73 @@
|
|||||||
#include <argp.h>
|
#include <argp.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include "arguments.h"
|
#include "arguments.h"
|
||||||
|
#include "log.h"
|
||||||
|
|
||||||
// function prototypes
|
// function prototypes
|
||||||
static error_t arg_parser( int key, char* arg, struct argp_state* state );
|
static error_t arg_parser( int key, char* arg, struct argp_state* state );
|
||||||
|
|
||||||
|
// constant definitions
|
||||||
|
const char* argp_program_version = "scan version 0.1";
|
||||||
|
|
||||||
// global variable definitions
|
// global variable definitions
|
||||||
const char* argp_program_version = "0.1";
|
|
||||||
static char description[] = "`scan` is an alternative to `ls` with better colour support and builtin tree view.";
|
static char description[] = "`scan` is an alternative to `ls` with better colour support and builtin tree view.";
|
||||||
static char argument_description[] = "ARGUMENTS:";
|
static char argument_description[] = "[path]";
|
||||||
|
|
||||||
static struct argp_option options[] =
|
static struct argp_option options[] =
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
"all",
|
.name = "all",
|
||||||
'a',
|
.key = 'a',
|
||||||
NULL,
|
.arg = NULL,
|
||||||
OPTION_ARG_OPTIONAL,
|
.flags = 0,
|
||||||
"show all items (including hidden ones)",
|
.doc = "show all items (including hidden ones)",
|
||||||
0
|
.group = 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"almost-all",
|
.name = "almost-all",
|
||||||
'A',
|
.key = 'A',
|
||||||
NULL,
|
.arg = NULL,
|
||||||
OPTION_ARG_OPTIONAL,
|
.flags = 0,
|
||||||
"same --all, excluding . and ..",
|
.doc = "same as `all`, but hiding `.` and `..`",
|
||||||
0
|
.group = 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"group-directories",
|
.name = "group-directories",
|
||||||
'g',
|
.key = 'g',
|
||||||
NULL,
|
.arg = NULL,
|
||||||
OPTION_ARG_OPTIONAL,
|
.flags = 0,
|
||||||
"group directories at the top",
|
.doc = "group directories at the top",
|
||||||
0
|
.group = 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"human-readable",
|
.name = "human-readable",
|
||||||
'h',
|
.key = 'h',
|
||||||
NULL,
|
.arg = NULL,
|
||||||
OPTION_ARG_OPTIONAL,
|
.flags = 0,
|
||||||
"show sizes in human readable format",
|
.doc = "show sizes in human readable format",
|
||||||
0
|
.group = 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"long",
|
.name = "long",
|
||||||
'l',
|
.key = 'l',
|
||||||
NULL,
|
.arg = NULL,
|
||||||
OPTION_ARG_OPTIONAL,
|
.flags = 0,
|
||||||
"use long listing format",
|
.doc = "use long listing format",
|
||||||
0
|
.group = 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"tree",
|
.name = "tree",
|
||||||
'T',
|
.key = 'T',
|
||||||
NULL,
|
.arg = NULL,
|
||||||
OPTION_ARG_OPTIONAL,
|
.flags = 0,
|
||||||
"use tree listing format",
|
.doc = "use tree listing format",
|
||||||
0
|
.group = 0
|
||||||
}
|
},
|
||||||
|
{ 0 } // this is required to properly end the options array ..
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct argp parser = {
|
static struct argp parser =
|
||||||
|
{
|
||||||
options,
|
options,
|
||||||
arg_parser,
|
arg_parser,
|
||||||
argument_description,
|
argument_description,
|
||||||
@@ -73,17 +79,58 @@ static struct argp parser = {
|
|||||||
|
|
||||||
|
|
||||||
// module logic
|
// module logic
|
||||||
|
void initialise_args( arguments_t* arguments )
|
||||||
|
{
|
||||||
|
arguments->show_all = false;
|
||||||
|
arguments->show_almost_all = false;
|
||||||
|
arguments->group_directories = false;
|
||||||
|
arguments->human_readable = false;
|
||||||
|
arguments->long_view = false;
|
||||||
|
arguments->tree_view = false;
|
||||||
|
}
|
||||||
|
|
||||||
error_t parse_args( int argc, char** argv, arguments_t* arguments )
|
error_t parse_args( int argc, char** argv, arguments_t* arguments )
|
||||||
{
|
{
|
||||||
// TODO: revisit ARG_INDEX for handling unknown arguments
|
int arg_index = 0;
|
||||||
return argp_parse(&parser, argc, argv, 0, NULL, arguments);
|
|
||||||
|
if ( 0 != argp_parse(&parser, argc, argv, 0, &arg_index, arguments ) )
|
||||||
|
{
|
||||||
|
log_error( "error! arg_index = %d;", arg_index );
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static error_t arg_parser( int key, char* arg, struct argp_state* state )
|
static error_t arg_parser( int key, char* arg, struct argp_state* state )
|
||||||
{
|
{
|
||||||
( void )key;
|
|
||||||
( void )arg;
|
( void )arg;
|
||||||
( void )state;
|
arguments_t* arguments = ( arguments_t* )( state->input );
|
||||||
|
|
||||||
|
if ( !state ) return -EINVAL;
|
||||||
|
|
||||||
|
switch ( key )
|
||||||
|
{
|
||||||
|
case 'a':
|
||||||
|
arguments->show_all = true;
|
||||||
|
break;
|
||||||
|
case 'A':
|
||||||
|
arguments->show_almost_all = true;
|
||||||
|
break;
|
||||||
|
case 'g':
|
||||||
|
arguments->group_directories = true;
|
||||||
|
break;
|
||||||
|
case 'h':
|
||||||
|
arguments->human_readable = true;
|
||||||
|
break;
|
||||||
|
case 'l':
|
||||||
|
arguments->long_view = true;
|
||||||
|
break;
|
||||||
|
case 't':
|
||||||
|
arguments->tree_view = true;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return ARGP_ERR_UNKNOWN;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
+7
-14
@@ -16,22 +16,11 @@ int main( int argc, char** argv )
|
|||||||
{
|
{
|
||||||
arguments_t args;
|
arguments_t args;
|
||||||
|
|
||||||
|
initialise_args( &args );
|
||||||
|
|
||||||
parse_args( argc, argv, &args );
|
parse_args( argc, argv, &args );
|
||||||
|
|
||||||
if ( 2 < argc ) return -1;
|
char argument_directory[ PATH_MAX ] = ".";
|
||||||
|
|
||||||
char argument_directory[ PATH_MAX ];
|
|
||||||
|
|
||||||
if ( 2 == argc )
|
|
||||||
{
|
|
||||||
strcpy( argument_directory, argv[1] );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
argument_directory[0] = '.';
|
|
||||||
argument_directory[1] = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
DIR* directory = opendir( argument_directory );
|
DIR* directory = opendir( argument_directory );
|
||||||
struct dirent* entry;
|
struct dirent* entry;
|
||||||
|
|
||||||
@@ -47,6 +36,10 @@ int main( int argc, char** argv )
|
|||||||
|
|
||||||
while ( NULL != ( entry = readdir( directory ) ) )
|
while ( NULL != ( entry = readdir( directory ) ) )
|
||||||
{
|
{
|
||||||
|
if ( !args.show_all && ( !strncmp( entry->d_name, ".", 1) || !strncmp( entry->d_name, "..", 2 ) ) )
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
log_info( "%s", entry->d_name );
|
log_info( "%s", entry->d_name );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user