diff mbox series

[v3,4/5] x86: Use getopt to handle command line args

Message ID 20240430124709.865183-5-fouad.hilly@cloud.com (mailing list archive)
State Superseded
Headers show
Series x86/xen-ucode: Introduce --force option | expand

Commit Message

Fouad Hilly April 30, 2024, 12:47 p.m. UTC
Use getopt_long() to handle command line arguments.
Introduce ext_err for common exit with errors.

Signed-off-by: Fouad Hilly <fouad.hilly@cloud.com>
---
[v3]
1- Exit with error prints erros only to stderr.
2- Remove argc checks.
3- Utilize optind.

[v2]
1- Removed unnecessary NULL initialization.
2- Used static at the beginning of type struct declaration.
3- Corrected switch\case indentations.
4- Removed redundant checks.
5- Corrected label indentation.
 tools/misc/xen-ucode.c | 45 +++++++++++++++++++++++++++++++-----------
 1 file changed, 33 insertions(+), 12 deletions(-)

Comments

Jan Beulich May 6, 2024, 8:21 a.m. UTC | #1
On 30.04.2024 14:47, Fouad Hilly wrote:
> Use getopt_long() to handle command line arguments.
> Introduce ext_err for common exit with errors.
> 
> Signed-off-by: Fouad Hilly <fouad.hilly@cloud.com>

Nit: Neither subject nor description make clear ...

>  tools/misc/xen-ucode.c | 45 +++++++++++++++++++++++++++++++-----------
>  1 file changed, 33 insertions(+), 12 deletions(-)

... what tool is actually being touched.

Jan
Fouad Hilly May 9, 2024, 1:59 p.m. UTC | #2
On Mon, May 6, 2024 at 9:21 AM Jan Beulich <jbeulich@suse.com> wrote:
>
> On 30.04.2024 14:47, Fouad Hilly wrote:
> > Use getopt_long() to handle command line arguments.
> > Introduce ext_err for common exit with errors.
> >
> > Signed-off-by: Fouad Hilly <fouad.hilly@cloud.com>
>
> Nit: Neither subject nor description make clear ...
>
> >  tools/misc/xen-ucode.c | 45 +++++++++++++++++++++++++++++++-----------
> >  1 file changed, 33 insertions(+), 12 deletions(-)
>
> ... what tool is actually being touched.

Noted and will be fixed in V4
>
> Jan
diff mbox series

Patch

diff --git a/tools/misc/xen-ucode.c b/tools/misc/xen-ucode.c
index 005bf85b6551..d95f967f021b 100644
--- a/tools/misc/xen-ucode.c
+++ b/tools/misc/xen-ucode.c
@@ -11,6 +11,7 @@ 
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <xenctrl.h>
+#include <getopt.h>
 
 static xc_interface *xch;
 
@@ -20,7 +21,10 @@  static const char   amd_id[] = "AuthenticAMD";
 static void usage(const char *name)
 {
     printf("%s: Xen microcode updating tool\n"
-           "Usage: %s [<microcode file> | show-cpu-info]\n",
+           "Usage: %s [<microcode file> | Options]\n"
+           "options:\n"
+           "  -h, --help            display this help and exit\n"
+           "  -s, --show-cpu-info   show CPU information and exit\n",
            name, name);
 }
 
@@ -84,6 +88,13 @@  int main(int argc, char *argv[])
     char *filename, *buf;
     size_t len;
     struct stat st;
+    int opt;
+
+    static const struct option options[] = {
+        {"help", no_argument, NULL, 'h'},
+        {"show-cpu-info", no_argument, NULL, 's'},
+        {NULL, no_argument, NULL, 0}
+    };
 
     xch = xc_interface_open(NULL, NULL, 0);
     if ( xch == NULL )
@@ -93,21 +104,25 @@  int main(int argc, char *argv[])
         exit(1);
     }
 
-    if ( argc < 2 )
+    while ( (opt = getopt_long(argc, argv, "hs", options, NULL)) != -1 )
     {
-        fprintf(stderr,
-                "%s: unable to process command line arguments\n", argv[0]);
-        usage(argv[0]);
-        exit(2);
+        switch (opt)
+        {
+        case 'h':
+            usage(argv[0]);
+            exit(EXIT_SUCCESS);
+        case 's':
+            show_curr_cpu(stdout);
+            exit(EXIT_SUCCESS);
+        default:
+            goto ext_err;
+        }
     }
 
-    if ( !strcmp(argv[1], "show-cpu-info") )
-    {
-        show_curr_cpu(stdout);
-        return 0;
-    }
+    if ( optind == argc )
+        goto ext_err;
 
-    filename = argv[1];
+    filename = argv[optind];
     fd = open(filename, O_RDONLY);
     if ( fd < 0 )
     {
@@ -149,4 +164,10 @@  int main(int argc, char *argv[])
     close(fd);
 
     return 0;
+
+ ext_err:
+    fprintf(stderr,
+            "%s: unable to process command line arguments\n", argv[0]);
+    usage(argv[0]);
+    exit(EXIT_FAILURE);
 }