started argument parsing using argp

This commit is contained in:
root committed 2026-06-22 23:16:45 +02:00
1 parent 7b1511e31a
commit 62542734b5
4 files changed
+130 -6

No files matched your search

+2 -1
View File
@@ -33,7 +33,8 @@ WCC := -Wconversion \
C_DEF := C_DEF :=
C_SRC := main.c \ C_SRC := main.c \
log.c log.c \
arguments.c
C_FLAGS := -std=c11 \ C_FLAGS := -std=c11 \
$(WG) \ $(WG) \
+24
View File
@@ -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
+89
View File
@@ -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
View File
@@ -1,16 +1,22 @@
// external dependencies
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <argp.h>
#include <dirent.h> #include <dirent.h>
#include <errno.h>
#include <linux/limits.h> #include <linux/limits.h>
// internal dependencies
#include "log.h" #include "log.h"
#include "arguments.h"
typedef struct dirent dirent; // program logic
int main( int argc, char** argv ) int main( int argc, char** argv )
{ {
( void )argc; arguments_t args;
( void )argv;
parse_args( argc, argv, &args );
if ( 2 < argc ) return -1; if ( 2 < argc ) return -1;
@@ -27,11 +33,15 @@ int main( int argc, char** argv )
} }
DIR* directory = opendir( argument_directory ); DIR* directory = opendir( argument_directory );
dirent* entry; struct dirent* entry;
if ( !directory ) if ( !directory )
{ {
if ( ENOENT == errno )
log_error( "no directory `%s`", argument_directory );
else
log_error( "failed to open directory `%s`", argument_directory ); log_error( "failed to open directory `%s`", argument_directory );
return -1; return -1;
} }