#include #include #include "arguments.h" #include "log.h" // function prototypes 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 static char description[] = "`scan` is an alternative to `ls` with better colour support and builtin tree view."; static char argument_description[] = "[path]"; static struct argp_option options[] = { { .name = "all", .key = 'a', .arg = NULL, .flags = 0, .doc = "show all items (including hidden ones)", .group = 0 }, { .name = "almost-all", .key = 'A', .arg = NULL, .flags = 0, .doc = "same as `all`, but hiding `.` and `..`", .group = 0 }, { .name = "group-directories", .key = 'g', .arg = NULL, .flags = 0, .doc = "group directories at the top", .group = 0 }, { .name = "human-readable", .key = 'h', .arg = NULL, .flags = 0, .doc = "show sizes in human readable format", .group = 0 }, { .name = "long", .key = 'l', .arg = NULL, .flags = 0, .doc = "use long listing format", .group = 0 }, { .name = "tree", .key = 'T', .arg = NULL, .flags = 0, .doc = "use tree listing format", .group = 0 }, { 0 } // this is required to properly end the options array .. }; static struct argp parser = { options, arg_parser, argument_description, description, NULL, NULL, NULL }; // 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 ) { int arg_index = 0; 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 ) { ( void )arg; 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; }