Command line usability improvements.
authorTom Clegg <tom@clinicalfuture.com>
Tue, 3 Dec 2013 06:53:56 +0000 (22:53 -0800)
committerTom Clegg <tom@clinicalfuture.com>
Tue, 3 Dec 2013 06:56:52 +0000 (22:56 -0800)
* Read from stdin if no path arguments are given.
* Add --stream, --manifest, --raw flags (equivalent to --as-*).

refs #1646

sdk/cli/bin/arv-put
sdk/cli/test/test_arv-put.rb

index 82c4c82dc66cdbc6abadc4ffa63c490b8334c9c7..bbd6d032103deb9306995bbf8a256cc038b447ae 100755 (executable)
@@ -9,8 +9,10 @@ import sys
 
 parser = argparse.ArgumentParser(
     description='Copy data from the local filesystem to Keep.')
-parser.add_argument('paths', metavar='path', type=str, nargs='+',
-                    help='Local file or directory.')
+parser.add_argument('paths', metavar='path', type=str, nargs='*',
+                    help="""
+Local file or directory. Default: read from standard input.
+""")
 parser.add_argument('--max-manifest-depth', type=int, metavar='N', default=-1,
                     help="""
 Maximum depth of directory tree to represent in the manifest
@@ -19,21 +21,33 @@ as a single stream in the manifest. If N=0, the manifest will contain
 a single stream. Default: -1 (unlimited), i.e., exactly one manifest
 stream per filesystem directory that contains files.
 """)
-parser.add_argument('--as-stream', action='store_true',
-                    help="""
+group = parser.add_mutually_exclusive_group()
+group.add_argument('--as-stream', action='store_true', dest='stream',
+                   help="""
+Synonym for --stream.
+""")
+group.add_argument('--stream', action='store_true',
+                   help="""
 Store the file content and display the resulting manifest on
 stdout. Do not write the manifest to Keep or save a Collection object
 in Arvados.
 """)
-parser.add_argument('--as-manifest', action='store_const',
-                    dest='as_stream', const=False, default=False,
-                    help="""
+group.add_argument('--as-manifest', action='store_true', dest='manifest',
+                   help="""
+Synonym for --manifest.
+""")
+group.add_argument('--manifest', action='store_true',
+                   help="""
 Store the file data and resulting manifest in Keep, save a Collection
 object in Arvados, and display the manifest locator (Collection uuid)
 on stdout. This is the default behavior.
 """)
-parser.add_argument('--as-raw', action='store_true',
-                    help="""
+group.add_argument('--as-raw', action='store_true', dest='raw',
+                   help="""
+Synonym for --raw.
+""")
+group.add_argument('--raw', action='store_true',
+                   help="""
 Store the file content and display the data block locators on stdout,
 separated by spaces, with a trailing newline. Do not store a manifest.
 """)
@@ -64,6 +78,9 @@ total data size).
 
 args = parser.parse_args()
 
+if len(args.paths) == 0:
+    args.paths += ['/dev/stdin']
+
 if args.filename and (len(args.paths) != 1 or os.path.isdir(args.paths[0])):
     parser.error("""
 --filename argument cannot be used when storing a directory or
@@ -151,9 +168,9 @@ for path in args.paths:
                     break
                 writer.write(buf)
 
-if args.as_stream:
+if args.stream:
     print writer.manifest_text(),
-elif args.as_raw:
+elif args.raw:
     writer.finish_current_stream()
     print string.join(writer.data_locators(), ' ') + '\n'
 else:
index 1172bf8416fc66d1071482c67046898ebe8bfaad..de619b9d92280339a23e4edb5c7f1cb4fa9de4a8 100644 (file)
@@ -15,15 +15,6 @@ class TestArvPut < Minitest::Test
     end
   end
 
-  def test_no_args
-    out, err = capture_subprocess_io do
-      assert_equal(false, arv_put,
-                   'arv-put without args exits non-zero')
-    end
-    assert_equal '', out
-    assert_match /^usage:/, err
-  end
-
   def test_help
     out, err = capture_subprocess_io do
       assert_equal(true, arv_put('-h'),
@@ -99,6 +90,10 @@ class TestArvPut < Minitest::Test
     assert_equal '', out
   end
 
+  def test_read_from_implicit_stdin
+    test_read_from_stdin(specify_stdin_as='--manifest')
+  end
+
   def test_read_from_dev_stdin
     test_read_from_stdin(specify_stdin_as='/dev/stdin')
   end