From 225c12aebf2d2f27e1d032d6b2149c7bb1d63506 Mon Sep 17 00:00:00 2001 From: Nikolaus Rath Date: Sun, 9 Oct 2016 17:08:29 -0700 Subject: [PATCH] fuse_parse_cmdline(): do not print help/version text The current behavior makes it difficult to add help for additional options. With the change, this becomes a lot easier. --- example/fuse_lo-plus.c | 15 +++++++++++++-- example/hello_ll.c | 14 ++++++++++++-- example/timefs1.c | 30 ++++++++++++++++++++++++++---- example/timefs2.c | 23 +++++++++++++++++++++-- example/timefs3.c | 24 ++++++++++++++++++++++-- include/fuse_lowlevel.h | 29 ++++++++++++++--------------- lib/fuse_versionscript | 1 + lib/helper.c | 38 +++----------------------------------- 8 files changed, 112 insertions(+), 62 deletions(-) diff --git a/example/fuse_lo-plus.c b/example/fuse_lo-plus.c index 60c2daf..c27f377 100644 --- a/example/fuse_lo-plus.c +++ b/example/fuse_lo-plus.c @@ -452,10 +452,21 @@ int main(int argc, char *argv[]) if (fuse_parse_cmdline(&args, &opts) != 0) return 1; - if (opts.show_help || opts.show_version) { - ret = 1; + if (opts.show_help) { + printf("usage: %s [options] \n\n", argv[0]); + fuse_cmdline_help(); + fuse_lowlevel_help(); + fuse_mount_help(); + ret = 0; + goto err_out1; + } else if (opts.show_version) { + printf("FUSE library version %s\n", fuse_pkgversion()); + fuse_lowlevel_version(); + fuse_mount_version(); + ret = 0; goto err_out1; } + lo.debug = opts.debug; lo.root.next = lo.root.prev = &lo.root; lo.root.fd = open("/", O_PATH); diff --git a/example/hello_ll.c b/example/hello_ll.c index 014b8ca..b830cb2 100644 --- a/example/hello_ll.c +++ b/example/hello_ll.c @@ -192,8 +192,18 @@ int main(int argc, char *argv[]) if (fuse_parse_cmdline(&args, &opts) != 0) return 1; - if (opts.show_help || opts.show_version) { - ret = 1; + if (opts.show_help) { + printf("usage: %s [options] \n\n", argv[0]); + fuse_cmdline_help(); + fuse_lowlevel_help(); + fuse_mount_help(); + ret = 0; + goto err_out1; + } else if (opts.show_version) { + printf("FUSE library version %s\n", fuse_pkgversion()); + fuse_lowlevel_version(); + fuse_mount_version(); + ret = 0; goto err_out1; } diff --git a/example/timefs1.c b/example/timefs1.c index cff9545..430e00f 100644 --- a/example/timefs1.c +++ b/example/timefs1.c @@ -273,6 +273,15 @@ static void* update_fs(void *data) { return NULL; } +static void show_help(const char *progname) +{ + printf("usage: %s [options] \n\n", progname); + printf("File-system specific options:\n" + " --update-interval= Update-rate of file system contents\n" + " --no-notify Disable kernel notifications\n" + "\n"); +} + int main(int argc, char *argv[]) { struct fuse_args args = FUSE_ARGS_INIT(argc, argv); struct fuse_session *se; @@ -284,13 +293,26 @@ int main(int argc, char *argv[]) { opt_proc) == -1) return 1; - if (fuse_parse_cmdline(&args, &opts) != 0) - return 1; - if (opts.show_help || opts.show_version) { + if (fuse_parse_cmdline(&args, &opts) != 0) { ret = 1; goto err_out1; } + if (opts.show_help) { + show_help(argv[0]); + fuse_cmdline_help(); + fuse_lowlevel_help(); + fuse_mount_help(); + ret = 0; + goto err_out1; + } else if (opts.show_version) { + printf("FUSE library version %s\n", fuse_pkgversion()); + fuse_lowlevel_version(); + fuse_mount_version(); + ret = 0; + goto err_out1; + } + se = fuse_session_new(&args, &tfs_oper, sizeof(tfs_oper), NULL); if (se == NULL) @@ -324,8 +346,8 @@ err_out3: err_out2: fuse_session_destroy(se); err_out1: - free(opts.mountpoint); fuse_opt_free_args(&args); + free(opts.mountpoint); return ret ? 1 : 0; } diff --git a/example/timefs2.c b/example/timefs2.c index 3194230..ea10cc1 100644 --- a/example/timefs2.c +++ b/example/timefs2.c @@ -319,6 +319,15 @@ static void* update_fs(void *data) { return NULL; } +static void show_help(const char *progname) +{ + printf("usage: %s [options] \n\n", progname); + printf("File-system specific options:\n" + " --update-interval= Update-rate of file system contents\n" + " --no-notify Disable kernel notifications\n" + "\n"); +} + int main(int argc, char *argv[]) { struct fuse_args args = FUSE_ARGS_INIT(argc, argv); struct fuse_session *se; @@ -332,8 +341,18 @@ int main(int argc, char *argv[]) { if (fuse_parse_cmdline(&args, &opts) != 0) return 1; - if (opts.show_help || opts.show_version) { - ret = 1; + if (opts.show_help) { + show_help(argv[0]); + fuse_cmdline_help(); + fuse_lowlevel_help(); + fuse_mount_help(); + ret = 0; + goto err_out1; + } else if (opts.show_version) { + printf("FUSE library version %s\n", fuse_pkgversion()); + fuse_lowlevel_version(); + fuse_mount_version(); + ret = 0; goto err_out1; } diff --git a/example/timefs3.c b/example/timefs3.c index 0a52e0f..c04b81c 100644 --- a/example/timefs3.c +++ b/example/timefs3.c @@ -253,6 +253,16 @@ static void* update_fs(void *data) { return NULL; } +static void show_help(const char *progname) +{ + printf("usage: %s [options] \n\n", progname); + printf("File-system specific options:\n" + " --timeout= Timeout for kernel caches\n" + " --update-interval= Update-rate of file system contents\n" + " --no-notify Disable kernel notifications\n" + "\n"); +} + int main(int argc, char *argv[]) { struct fuse_args args = FUSE_ARGS_INIT(argc, argv); struct fuse_session *se; @@ -266,8 +276,18 @@ int main(int argc, char *argv[]) { if (fuse_parse_cmdline(&args, &opts) != 0) return 1; - if (opts.show_help || opts.show_version) { - ret = 1; + if (opts.show_help) { + show_help(argv[0]); + fuse_cmdline_help(); + fuse_lowlevel_help(); + fuse_mount_help(); + ret = 0; + goto err_out1; + } else if (opts.show_version) { + printf("FUSE library version %s\n", fuse_pkgversion()); + fuse_lowlevel_version(); + fuse_mount_version(); + ret = 0; goto err_out1; } diff --git a/include/fuse_lowlevel.h b/include/fuse_lowlevel.h index a834eba..4c39442 100644 --- a/include/fuse_lowlevel.h +++ b/include/fuse_lowlevel.h @@ -1599,6 +1599,12 @@ void fuse_lowlevel_help(void); */ void fuse_mount_help(void); + +/** + * Print available options for `fuse_parse_cmdline()`. + */ +void fuse_cmdline_help(void); + /* ----------------------------------------------------------- * * Filesystem setup & teardown * * ----------------------------------------------------------- */ @@ -1615,21 +1621,14 @@ struct fuse_cmdline_opts { /** * Utility function to parse common options for simple file systems - * using the low-level API. Available options are listed in `struct - * fuse_opt fuse_helper_opts[]`. A single non-option argument is - * treated as the mountpoint. Multiple (or no) non-option arguments - * will result in an error. - * - * Unknown options are passed through unchanged. Known options (other - * than --debug, which is preserved) and the mountpoint argument are - * removed from *args*. - * - * If --help or --version is specified, the appropriate information is - * printed to stdout and the function returns -1. - * - * If neither -o subtype= or -o fsname= options are given, the subtype - * is set to the basename of the program (the fsname defaults to - * "fuse"). + * using the low-level API. A help text that describes the available + * options can be printed with `fuse_cmdline_help`. A single + * non-option argument is treated as the mountpoint. Multiple + * non-option arguments will result in an error. + * + * If neither -o subtype= or -o fsname= options are given, a new + * subtype option will be added and set to the basename of the program + * (the fsname will remain unset, and then defaults to "fuse"). * * @param args argument vector (input+output) * @param opts output argument for parsed options diff --git a/lib/fuse_versionscript b/lib/fuse_versionscript index 14eb362..3301b12 100644 --- a/lib/fuse_versionscript +++ b/lib/fuse_versionscript @@ -127,6 +127,7 @@ FUSE_3.0 { fuse_lowlevel_help; fuse_lowlevel_version; fuse_mount_help; + fuse_cmdline_help; fuse_mount_version; local: diff --git a/lib/helper.c b/lib/helper.c index 140de00..da93413 100644 --- a/lib/helper.c +++ b/lib/helper.c @@ -48,12 +48,7 @@ static const struct fuse_opt fuse_helper_opts[] = { FUSE_OPT_END }; -static void usage(const char *progname) -{ - printf("usage: %s mountpoint [options]\n\n", progname); -} - -static void helper_help(void) +void fuse_cmdline_help(void) { printf("General options:\n" " -h --help print help\n" @@ -64,11 +59,6 @@ static void helper_help(void) "\n"); } -static void helper_version(void) -{ - printf("FUSE library version: %s\n", PACKAGE_VERSION); -} - static int fuse_helper_opt_proc(void *data, const char *arg, int key, struct fuse_args *outargs) { @@ -126,27 +116,6 @@ int fuse_parse_cmdline(struct fuse_args *args, fuse_helper_opt_proc) == -1) return -1; - if (opts->show_version) { - helper_version(); - fuse_lowlevel_version(); - fuse_mount_version(); - return -1; - } - - if (opts->show_help) { - usage(args->argv[0]); - helper_help(); - fuse_lowlevel_help(); - fuse_mount_help(); - return -1; - } - - if (!opts->mountpoint) { - fprintf(stderr, "error: no mountpoint specified\n"); - usage(args->argv[0]); - return -1; - } - /* If neither -o subtype nor -o fsname are specified, set subtype to program's basename */ if (!opts->nodefault_subtype) @@ -225,7 +194,7 @@ int fuse_main_real(int argc, char *argv[], const struct fuse_operations *op, return 1; if (opts.show_version) { - helper_version(); + printf("FUSE library version %s\n", PACKAGE_VERSION); fuse_lowlevel_version(); fuse_mount_version(); res = 0; @@ -235,7 +204,7 @@ int fuse_main_real(int argc, char *argv[], const struct fuse_operations *op, /* Re-add --help for later processing by fuse_new() (that way we also get help for modules options) */ if (opts.show_help) { - helper_help(); + fuse_cmdline_help(); if (fuse_opt_add_arg(&args, "--help") == -1) { res = 1; goto out1; @@ -245,7 +214,6 @@ int fuse_main_real(int argc, char *argv[], const struct fuse_operations *op, if (!opts.show_help && !opts.mountpoint) { fprintf(stderr, "error: no mountpoint specified\n"); - usage(args.argv[0]); res = 1; goto out1; } -- 2.30.2