]> arthur.barton.de Git - bup.git/commitdiff
Add an options.fatal() function and use it.
authorAvery Pennarun <apenwarr@gmail.com>
Sun, 28 Feb 2010 18:07:48 +0000 (13:07 -0500)
committerAvery Pennarun <apenwarr@gmail.com>
Sun, 28 Feb 2010 19:17:59 +0000 (14:17 -0500)
Every existing call to o.usage() was preceded by an error message that
printed the exename, then the error message.  So let's add a fatal()
function that does it all in one step.  This reduces the net number of lines
plus improves consistency.

15 files changed:
bup.py
cmd-damage.py
cmd-drecurse.py
cmd-fuse.py
cmd-index.py
cmd-init.py
cmd-margin.py
cmd-midx.py
cmd-random.py
cmd-save.py
cmd-server.py
cmd-split.py
cmd-tick.py
memtest.py
options.py

diff --git a/bup.py b/bup.py
index 56c6cd0c5c17230cb6ef3c7bb0082c5398561676..47bfb2d03ca52298196794bc0069c1b0689ae69f 100755 (executable)
--- a/bup.py
+++ b/bup.py
@@ -9,7 +9,7 @@ def log(s):
     sys.stderr.write(s)
 
 def usage():
-    log('Usage: %s <subcmd> <options...>\n\n' % exe)
+    log('Usage: bup <subcmd> <options...>\n\n')
     log('Available subcommands:\n')
     for c in sorted(os.listdir(exepath)):
         if c.startswith('bup-') and c.find('.') < 0:
@@ -26,7 +26,7 @@ if subcmd == 'help':
 subpath = os.path.join(exepath, 'bup-%s' % subcmd)
 
 if not os.path.exists(subpath):
-    log('%s: unknown command "%s"\n' % (exe, subcmd))
+    log('error: unknown command "%s"\n' % subcmd)
     usage()
 
 try:
index 1ca2cdecb25d1b003c2d01429b6e7b8334d788fa..685a109ea9bd255f0dd34a0814301d8acb61ee63 100755 (executable)
@@ -25,7 +25,7 @@ o = options.Options('bup damage', optspec)
 (opt, flags, extra) = o.parse(sys.argv[1:])
 
 if not extra:
-    o.usage()
+    o.fatal('filenames expected')
 
 if opt.seed != None:
     random.seed(opt.seed)
index f09f0411b2e65adaecae58bbd62f7fd0a2d8ee9b..c2b70794bf6e80349026f69bc00928aaba6535a1 100755 (executable)
@@ -13,8 +13,7 @@ o = options.Options('bup drecurse', optspec)
 (opt, flags, extra) = o.parse(sys.argv[1:])
 
 if len(extra) != 1:
-    log("drecurse: exactly one filename expected\n")
-    o.usage()
+    o.fatal("exactly one filename expected")
 
 it = drecurse.recursive_dirlist(extra, opt.xdev)
 if opt.profile:
index f6893c6b77e43cc16c4b28a621d6cf35f699eef0..0b0bab64ce286dce8125116b96c3e402bbd3ed64 100755 (executable)
@@ -105,8 +105,7 @@ o = options.Options('bup fuse', optspec)
 (opt, flags, extra) = o.parse(sys.argv[1:])
 
 if len(extra) != 1:
-    log("bup fuse: exactly one argument expected\n")
-    o.usage()
+    o.fatal("exactly one argument expected")
 
 git.check_repo_or_die()
 top = vfs.RefList(None)
index b3f003b378e68b35f0a340ab6d4e643b0f370e3b..735eb7db5313cd6357fc8cc425fdb8b087abf0f5 100755 (executable)
@@ -128,14 +128,11 @@ o = options.Options('bup index', optspec)
 (opt, flags, extra) = o.parse(sys.argv[1:])
 
 if not (opt.modified or opt['print'] or opt.status or opt.update or opt.check):
-    log('bup index: supply one or more of -p, -s, -m, -u, or --check\n')
-    o.usage()
+    o.fatal('supply one or more of -p, -s, -m, -u, or --check')
 if (opt.fake_valid or opt.fake_invalid) and not opt.update:
-    log('bup index: --fake-{in,}valid are meaningless without -u\n')
-    o.usage()
+    o.fatal('--fake-{in,}valid are meaningless without -u')
 if opt.fake_valid and opt.fake_invalid:
-    log('bup index: --fake-valid is incompatible with --fake-invalid\n')
-    o.usage()
+    o.fatal('--fake-valid is incompatible with --fake-invalid')
 
 git.check_repo_or_die()
 indexfile = opt.indexfile or git.repo('bupindex')
@@ -148,8 +145,7 @@ paths = index.reduce_paths(extra)
 
 if opt.update:
     if not paths:
-        log('bup index: update (-u) requested but no paths given\n')
-        o.usage()
+        o.fatal('update (-u) requested but no paths given')
     for (rp,path) in paths:
         update_index(rp)
 
index 9f29b1cc3d4c38ee1fe6ffd255701845a9aa454d..8dca178bd7d30475fd267a90a57c564811b97162 100755 (executable)
@@ -11,8 +11,7 @@ o = options.Options('bup init', optspec)
 (opt, flags, extra) = o.parse(sys.argv[1:])
 
 if extra:
-    log("bup init: no arguments expected\n")
-    o.usage()
+    o.fatal("no arguments expected")
 
 
 if opt.remote:
index b7f5bb24671278cc360038ba7dcc07de2c13214f..05f7d2b68e30bcce97b5a194645350b026672947 100755 (executable)
@@ -11,8 +11,7 @@ o = options.Options('bup margin', optspec)
 (opt, flags, extra) = o.parse(sys.argv[1:])
 
 if extra:
-    log("bup margin: no arguments expected\n")
-    o.usage()
+    o.fatal("no arguments expected")
 
 git.check_repo_or_die()
 #git.ignore_midx = 1
index 6f62e66d59cdc2da7c598dba8de9ccd63b66dcb1..36907ebaa0ee7569e68a652f221a12119eee5559 100755 (executable)
@@ -86,8 +86,7 @@ o = options.Options('bup midx', optspec)
 (opt, flags, extra) = o.parse(sys.argv[1:])
 
 if extra and (opt.auto or opt.force):
-    log("bup midx: you can't use -f/-a and also provide filenames\n")
-    o.usage()
+    o.fatal("you can't use -f/-a and also provide filenames")
 
 git.check_repo_or_die()
 
@@ -110,5 +109,4 @@ elif opt.auto or opt.force:
             do_midx(path, opt.output, needed.keys())
         log('\n')
 else:
-    log("bup midx: you must use -f or -a or provide input filenames\n")
-    o.usage()
+    o.fatal("you must use -f or -a or provide input filenames")
index dd61cf0924cc45afdaf8e88bd9eb0f9515113146..518cea8bb5ba7247c2fbeb72599a7b28df41c67e 100755 (executable)
@@ -12,8 +12,7 @@ o = options.Options('bup random', optspec)
 (opt, flags, extra) = o.parse(sys.argv[1:])
 
 if len(extra) != 1:
-    log("bup random: exactly one argument expected\n")
-    o.usage()
+    o.fatal("exactly one argument expected")
 
 total = parse_num(extra[0])
 _hashsplit.write_random(sys.stdout.fileno(), total, opt.seed or 0)
index 81a663b1c33c4dd83e60fa467448c2642ec775e5..2f73a9b88400ea9eee6963cb9153141d9396854a 100755 (executable)
@@ -20,11 +20,9 @@ o = options.Options('bup save', optspec)
 
 git.check_repo_or_die()
 if not (opt.tree or opt.commit or opt.name):
-    log("bup save: use one or more of -t, -c, -n\n")
-    o.usage()
+    o.fatal("use one or more of -t, -c, -n")
 if not extra:
-    log("bup save: no filenames given.\n")
-    o.usage()
+    o.fatal("no filenames given")
 
 opt.progress = (istty and not opt.quiet)
 
index e849b451d627d7de5ebdb7b86101ac432883fdb9..92aa8c1dfadb479f1bef507b36cd23559d8244f1 100755 (executable)
@@ -138,8 +138,7 @@ o = options.Options('bup server', optspec)
 (opt, flags, extra) = o.parse(sys.argv[1:])
 
 if extra:
-    log('bup server: no arguments expected\n')
-    o.usage()
+    o.fatal('no arguments expected')
 
 log('bup server: reading from stdin.\n')
 
index 8b763910c6f9372794e8d4d0ed91d6ac8284439a..ae7b3806f42171bb78379462006f60356c1fc63d 100755 (executable)
@@ -28,12 +28,10 @@ o = options.Options('bup split', optspec)
 git.check_repo_or_die()
 if not (opt.blobs or opt.tree or opt.commit or opt.name or
         opt.noop or opt.copy):
-    log("bup split: use one or more of -b, -t, -c, -n, -N, --copy\n")
-    o.usage()
+    o.fatal("use one or more of -b, -t, -c, -n, -N, --copy")
 if (opt.noop or opt.copy) and (opt.blobs or opt.tree or 
                                opt.commit or opt.name):
-    log('bup split: -N is incompatible with -b, -t, -c, -n\n')
-    o.usage()
+    o.fatal('-N is incompatible with -b, -t, -c, -n')
 
 if opt.verbose >= 2:
     git.verbose = opt.verbose - 1
index ad9f708d6ff5150e8d23ca15e3f0b61ba3ee8942..12bd97eb077056c3f1c8630d9247a50b729aed6b 100755 (executable)
@@ -9,8 +9,7 @@ o = options.Options('bup tick', optspec)
 (opt, flags, extra) = o.parse(sys.argv[1:])
 
 if extra:
-    log("bup tick: no arguments expected\n")
-    o.usage()
+    o.fatal("no arguments expected")
 
 t = time.time()
 tleft = 1 - (t - int(t))
index de24163bd95ac69dfe0ce0b85bac50e48dc1dd83..7595259f9422c7ba12dc473bfff084d9508bf649 100755 (executable)
@@ -34,7 +34,7 @@ o = options.Options(sys.argv[0], optspec)
 (opt, flags, extra) = o.parse(sys.argv[1:])
 
 if extra:
-    o.usage()
+    o.fatal('no arguments expected')
 
 git.ignore_midx = opt.ignore_midx
 
index ace93b2595b0388fff396c2e279452a5752ce36b..165016c27922e7bd2a2e2f30a7ca2a372c94fbb2 100644 (file)
@@ -73,19 +73,22 @@ class Options:
                 out.append(argtext + '\n')
             else:
                 out.append('\n')
-        return ''.join(out)
+        return ''.join(out).rstrip() + '\n'
     
     def usage(self):
         log(self._usagestr)
         sys.exit(97)
+
+    def fatal(self, s):
+        log('error: %s\n' % s)
+        return self.usage()
         
     def parse(self, args):
         try:
             (flags,extra) = getopt.gnu_getopt(args,
                                               self._shortopts, self._longopts)
         except getopt.GetoptError, e:
-            log('%s: %s\n' % (self.exe, e))
-            self.usage()
+            self.fatal(e)
 
         opt = OptDict()
         for f in self._aliases.values():