[systemd-devel] [PATCH] systemd-analyze: switch to python getopt for argument parsing
Shawn Landden
shawnlandden at gmail.com
Thu May 3 17:04:55 PDT 2012
this uses gnu style getopt, so you can put the opts at the end: (e.g.)
systemd-analyze blame --user
---
src/analyze/systemd-analyze | 38 ++++++++++++++++++++++++--------------
1 file changed, 24 insertions(+), 14 deletions(-)
diff --git a/src/analyze/systemd-analyze b/src/analyze/systemd-analyze
index ad7bd9a..ec68da1 100755
--- a/src/analyze/systemd-analyze
+++ b/src/analyze/systemd-analyze
@@ -1,6 +1,6 @@
#!/usr/bin/python
-import dbus, sys
+import getopt, dbus, sys
def acquire_time_data():
@@ -68,7 +68,7 @@ def draw_text(context, x, y, text, size = 12, r = 0, g = 0, b = 0, vcenter = 0.5
context.restore()
-def help():
+def usage():
sys.stdout.write("""systemd-analyze [--user] time
systemd-analyze [--user] blame
systemd-analyze [--user] plot
@@ -78,16 +78,24 @@ Process systemd profiling information
-h --help Show this help
""")
-
bus = dbus.SystemBus()
-command_index = 1
-
-if len(sys.argv) > 1 and sys.argv[1] == '--user':
- bus = dbus.SessionBus()
- command_index = 2
+try:
+ opts, args = getopt.gnu_getopt(sys.argv[1:], "h", ["help", "user"])
+except getopt.GetoptError, err:
+ print str(err)
+ usage()
+ sys.exit(2)
+for o, a in opts:
+ if o in ("-h", "--help"):
+ usage()
+ sys.exit()
+ if o == '--user':
+ bus = dbus.SessionBus()
+ else:
+ assert False, "unhandled option"
-if len(sys.argv) <= command_index or sys.argv[command_index] == 'time':
+if len(args) <= 0 or args[0] == 'time':
initrd_time, start_time, finish_time = acquire_start_time()
@@ -104,7 +112,7 @@ if len(sys.argv) <= command_index or sys.argv[command_index] == 'time':
finish_time/1000)
-elif sys.argv[command_index] == 'blame':
+elif args[0] == 'blame':
data = acquire_time_data()
s = sorted(data, key = lambda i: i[2] - i[1], reverse = True)
@@ -119,7 +127,7 @@ elif sys.argv[command_index] == 'blame':
sys.stdout.write("%6lums %s\n" % ((aet - ixt) / 1000, name))
-elif sys.argv[command_index] == 'plot':
+elif args[0] == 'plot':
import cairo, os
initrd_time, start_time, finish_time = acquire_start_time()
@@ -275,8 +283,10 @@ elif sys.argv[command_index] == 'plot':
finish_time/1000), hcenter = 0, vcenter = -1)
surface.finish()
-elif sys.argv[command_index] in ("help", "--help", "-h"):
- help()
+
+elif args[0] == 'help':
+ usage()
+ sys.exit()
else:
- sys.stderr.write("Unknown verb '%s'.\n" % sys.argv[command_index])
+ sys.stderr.write("Unknown verb '%s'.\n" % args[0])
sys.exit(1)
--
1.7.9.5
More information about the systemd-devel
mailing list