started argument parsing using argp
This commit is contained in:
1 parent
7b1511e31a
commit
62542734b5
4 files changed
+130
-6
No files matched your search
@@ -33,7 +33,8 @@ WCC := -Wconversion \
|
||||
C_DEF :=
|
||||
|
||||
C_SRC := main.c \
|
||||
log.c
|
||||
log.c \
|
||||
arguments.c
|
||||
|
||||
C_FLAGS := -std=c11 \
|
||||
$(WG) \
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/* --------------------------------------------------------
|
||||
* -- project: SCAN
|
||||
* -- description: handle commandline arguments
|
||||
* --------------------------------------------------------
|
||||
*/
|
||||
|
||||
#ifndef __ARGUMENTS_H
|
||||
#define __ARGUMENTS_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <argp.h>
|
||||
|
||||
typedef struct {
|
||||
bool show_all;
|
||||
bool show_almost_all;
|
||||
bool group_directories;
|
||||
bool human_readable;
|
||||
bool long_view;
|
||||
bool tree_view;
|
||||
} arguments_t;
|
||||
|
||||
error_t parse_args( int argc, char** argv, arguments_t* arguments );
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,89 @@
|
||||
#include <argp.h>
|
||||
#include "arguments.h"
|
||||
|
||||
// function prototypes
|
||||
static error_t arg_parser( int key, char* arg, struct argp_state* state );
|
||||
|
||||
// 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 argument_description[] = "ARGUMENTS:";
|
||||
|
||||
static struct argp_option options[] =
|
||||
{
|
||||
{
|
||||
"all",
|
||||
'a',
|
||||
NULL,
|
||||
OPTION_ARG_OPTIONAL,
|
||||
"show all items (including hidden ones)",
|
||||
0
|
||||
},
|
||||
{
|
||||
"almost-all",
|
||||
'A',
|
||||
NULL,
|
||||
OPTION_ARG_OPTIONAL,
|
||||
"same --all, excluding . and ..",
|
||||
0
|
||||
},
|
||||
{
|
||||
"group-directories",
|
||||
'g',
|
||||
NULL,
|
||||
OPTION_ARG_OPTIONAL,
|
||||
"group directories at the top",
|
||||
0
|
||||
},
|
||||
{
|
||||
"human-readable",
|
||||
'h',
|
||||
NULL,
|
||||
OPTION_ARG_OPTIONAL,
|
||||
"show sizes in human readable format",
|
||||
0
|
||||
},
|
||||
{
|
||||
"long",
|
||||
'l',
|
||||
NULL,
|
||||
OPTION_ARG_OPTIONAL,
|
||||
"use long listing format",
|
||||
0
|
||||
},
|
||||
{
|
||||
"tree",
|
||||
'T',
|
||||
NULL,
|
||||
OPTION_ARG_OPTIONAL,
|
||||
"use tree listing format",
|
||||
0
|
||||
}
|
||||
};
|
||||
|
||||
static struct argp parser = {
|
||||
options,
|
||||
arg_parser,
|
||||
argument_description,
|
||||
description,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
|
||||
|
||||
// module logic
|
||||
error_t parse_args( int argc, char** argv, arguments_t* arguments )
|
||||
{
|
||||
// TODO: revisit ARG_INDEX for handling unknown arguments
|
||||
return argp_parse(&parser, argc, argv, 0, NULL, arguments);
|
||||
}
|
||||
|
||||
static error_t arg_parser( int key, char* arg, struct argp_state* state )
|
||||
{
|
||||
( void )key;
|
||||
( void )arg;
|
||||
( void )state;
|
||||
|
||||
return 0;
|
||||
}
|
||||
+15
-5
@@ -1,16 +1,22 @@
|
||||
// external dependencies
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <argp.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <linux/limits.h>
|
||||
|
||||
// internal dependencies
|
||||
#include "log.h"
|
||||
#include "arguments.h"
|
||||
|
||||
typedef struct dirent dirent;
|
||||
|
||||
// program logic
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
( void )argc;
|
||||
( void )argv;
|
||||
arguments_t args;
|
||||
|
||||
parse_args( argc, argv, &args );
|
||||
|
||||
if ( 2 < argc ) return -1;
|
||||
|
||||
@@ -27,11 +33,15 @@ int main( int argc, char** argv )
|
||||
}
|
||||
|
||||
DIR* directory = opendir( argument_directory );
|
||||
dirent* entry;
|
||||
struct dirent* entry;
|
||||
|
||||
if ( !directory )
|
||||
{
|
||||
if ( ENOENT == errno )
|
||||
log_error( "no directory `%s`", argument_directory );
|
||||
else
|
||||
log_error( "failed to open directory `%s`", argument_directory );
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user