[Libguestfs] [libnbd PATCH 2/3] nbdsh: Improve --help and initial banner contents.

Eric Blake eblake at redhat.com
Wed Oct 26 21:16:00 UTC 2022


Document all options in --help output.  If -n is not in use, then
enhance the banner to print the current state of h, and further tailor
the advice given on useful next steps to take to mention opt_go when
using --opt-mode.
---
 python/nbdsh.py | 37 +++++++++++++++++++++++++++----------
 1 file changed, 27 insertions(+), 10 deletions(-)

diff --git a/python/nbdsh.py b/python/nbdsh.py
index 84ac84e2..f23d8bfb 100644
--- a/python/nbdsh.py
+++ b/python/nbdsh.py
@@ -61,11 +61,13 @@ def shell():
     # For back-compat, provide --connect as an undocumented synonym to --uri
     parser.add_argument('--connect', dest='uri', help=argparse.SUPPRESS)

-    parser.add_argument('-v', '--verbose', action='store_true')
+    parser.add_argument('-v', '--verbose', action='store_true',
+                        help="enable verbose debugging")
     short_options.append("-v")
     long_options.append("--verbose")

-    parser.add_argument('-V', '--version', action='store_true')
+    parser.add_argument('-V', '--version', action='store_true',
+                        help="display version information")
     short_options.append("-V")
     long_options.append("--version")

@@ -140,30 +142,45 @@ def shell():
     # If there are no explicit -c or --command parameters, go interactive.
     if len(args.command) - shortcuts == 0:
         sys.ps1 = "nbd> "
-        code.interact(banner=make_banner(args), local=locals(), exitmsg='')
+        if args.n:
+            h = None
+        code.interact(banner=make_banner(args, h), local=locals(), exitmsg='')


-def make_banner(args):
+def make_banner(args, h):
     lines = []
     def line(x): lines.append(x)
     def blank(): line("")
     def example(ex, desc): line("%-34s # %s" % (ex, desc))

+    connect_hint = False
+    go_hint = False
     blank()
     line("Welcome to nbdsh, the shell for interacting with")
     line("Network Block Device (NBD) servers.")
     blank()
-    if not args.n:
-        line("The ‘nbd’ module has already been imported and there")
-        line("is an open NBD handle called ‘h’.")
-        blank()
-    else:
+    if args.n:
         line("The ‘nbd’ module has already been imported.")
         blank()
         example("h = nbd.NBD()", "Create a new handle.")
-    if args.uri is None:
+        connect_hint = True
+    else:
+        state = h.connection_state()
+        state = state[:state.find(':')]
+        line("The ‘nbd’ module has already been imported and there")
+        line("is an open NBD handle called ‘h’ in state '%s'." % state)
+        blank()
+        if h.aio_is_created():
+            connect_hint = True
+            if h.get_opt_mode():
+                go_hint = True
+        elif h.aio_is_negotiating():
+            go_hint = True
+    if connect_hint:
         example('h.connect_tcp("remote", "10809")',
                 "Connect to a remote server.")
+    if go_hint:
+        example("h.opt_go()", "Finish option negotiation")
     example("h.get_size()", "Get size of the remote disk.")
     example("buf = h.pread(512, 0)", "Read the first sector.")
     example("exit() or Ctrl-D", "Quit the shell")
-- 
2.37.3



More information about the Libguestfs mailing list