usertools/hugepages: show usage if no action specified

Message ID 20221104113018.4069058-1-thomas@monjalon.net (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series usertools/hugepages: show usage if no action specified |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/iol-mellanox-Performance success Performance Testing PASS
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/iol-aarch64-unit-testing success Testing PASS
ci/iol-aarch64-compile-testing success Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-x86_64-unit-testing success Testing PASS
ci/iol-x86_64-compile-testing success Testing PASS
ci/github-robot: build success github build: passed

Commit Message

Thomas Monjalon Nov. 4, 2022, 11:30 a.m. UTC
  Previously, the script was doing nothing if no argument was provided.

If neither show, mount/unmount, clear/reserve are specified,
it is assumed that the user does not know how to use the script.
So the usage is printed and an error code is used in exit.
The user will understand something is wrong,
and can recall the script with the option -h to get more information.

Signed-off-by: Thomas Monjalon <thomas@monjalon.net>
---
 usertools/dpdk-hugepages.py | 4 ++++
 1 file changed, 4 insertions(+)
  

Comments

Robin Jarry Nov. 7, 2022, 9:39 a.m. UTC | #1
Thomas Monjalon, Nov 04, 2022 at 12:30:
> Previously, the script was doing nothing if no argument was provided.
>
> If neither show, mount/unmount, clear/reserve are specified,
> it is assumed that the user does not know how to use the script.
> So the usage is printed and an error code is used in exit.
> The user will understand something is wrong,
> and can recall the script with the option -h to get more information.
>
> Signed-off-by: Thomas Monjalon <thomas@monjalon.net>
> ---
>  usertools/dpdk-hugepages.py | 4 ++++
>  1 file changed, 4 insertions(+)
>
> diff --git a/usertools/dpdk-hugepages.py b/usertools/dpdk-hugepages.py
> index a22d504d3a..823cfcf185 100755
> --- a/usertools/dpdk-hugepages.py
> +++ b/usertools/dpdk-hugepages.py
> @@ -272,6 +272,10 @@ def main():
>          args.reserve = args.setup
>          args.mount = True
>  
> +    if not (args.show or args.mount or args.unmount or args.clear or args.reserve):
> +        parser.print_usage()
> +        sys.exit(1)

Hi Thomas,

I believe you can do:

           parser.error("at least one of -s/-c/-m/-u/-r/--setup is required")

and omit sys.exit(1).

$ ~/dev/dpdk/usertools/dpdk-hugepages.py
usage: dpdk-hugepages.py [-h] [--show] [--clear] [--mount] [--unmount] [--node NODE] [--pagesize SIZE] [--reserve SIZE] [--setup SIZE]
dpdk-hugepages.py: error: at least one of -s/-c/-m/-u/-r/--setup is required
  
Thomas Monjalon Nov. 22, 2022, 3:22 p.m. UTC | #2
07/11/2022 10:39, Robin Jarry:
> Thomas Monjalon, Nov 04, 2022 at 12:30:
> > Previously, the script was doing nothing if no argument was provided.
> >
> > If neither show, mount/unmount, clear/reserve are specified,
> > it is assumed that the user does not know how to use the script.
> > So the usage is printed and an error code is used in exit.
> > The user will understand something is wrong,
> > and can recall the script with the option -h to get more information.
> >
> > Signed-off-by: Thomas Monjalon <thomas@monjalon.net>
> > ---
> >  usertools/dpdk-hugepages.py | 4 ++++
> >  1 file changed, 4 insertions(+)
> >
> > diff --git a/usertools/dpdk-hugepages.py b/usertools/dpdk-hugepages.py
> > index a22d504d3a..823cfcf185 100755
> > --- a/usertools/dpdk-hugepages.py
> > +++ b/usertools/dpdk-hugepages.py
> > @@ -272,6 +272,10 @@ def main():
> >          args.reserve = args.setup
> >          args.mount = True
> >  
> > +    if not (args.show or args.mount or args.unmount or args.clear or args.reserve):
> > +        parser.print_usage()
> > +        sys.exit(1)
> 
> Hi Thomas,
> 
> I believe you can do:
> 
>            parser.error("at least one of -s/-c/-m/-u/-r/--setup is required")
> 
> and omit sys.exit(1).
> 
> $ ~/dev/dpdk/usertools/dpdk-hugepages.py
> usage: dpdk-hugepages.py [-h] [--show] [--clear] [--mount] [--unmount] [--node NODE] [--pagesize SIZE] [--reserve SIZE] [--setup SIZE]
> dpdk-hugepages.py: error: at least one of -s/-c/-m/-u/-r/--setup is required

It is a long message and not long enough to be accurate (long options are missing).
I would go with parser.error("no action specified")
  

Patch

diff --git a/usertools/dpdk-hugepages.py b/usertools/dpdk-hugepages.py
index a22d504d3a..823cfcf185 100755
--- a/usertools/dpdk-hugepages.py
+++ b/usertools/dpdk-hugepages.py
@@ -272,6 +272,10 @@  def main():
         args.reserve = args.setup
         args.mount = True
 
+    if not (args.show or args.mount or args.unmount or args.clear or args.reserve):
+        parser.print_usage()
+        sys.exit(1)
+
     if args.pagesize:
         pagesize_kb = get_memsize(args.pagesize)
     else: