diff --git a/scuba/__main__.py b/scuba/__main__.py
index 57f8671..4a54fae 100755
--- a/scuba/__main__.py
+++ b/scuba/__main__.py
@@ -217,7 +217,7 @@ def main(argv=None):
 
     if g_verbose or scuba_args.dry_run:
         appmsg('Docker command line:')
-        print(format_cmdline(run_args))
+        print('$ ' + format_cmdline(run_args))
 
     if scuba_args.dry_run:
         appmsg('Exiting for dry run. Temporary files not removed:')
diff --git a/scuba/utils.py b/scuba/utils.py
index f4d742b..0bd2e0e 100644
--- a/scuba/utils.py
+++ b/scuba/utils.py
@@ -3,13 +3,33 @@ try:
 except ImportError:
     from pipes import quote as shell_quote
 
+
 def format_cmdline(args, maxwidth=80):
+    '''Format args into a shell-quoted command line.
+
+    The result will be wrapped to maxwidth characters where possible,
+    not breaking a single long argument.
+    '''
+
+    # Leave room for the space and backslash at the end of each line
+    maxwidth -= 2
+
     def lines():
         line = ''
         for a in (shell_quote(a) for a in args):
-            if len(line) + len(a) > maxwidth:
+            # If adding this argument will make the line too long,
+            # yield the current line, and start a new one.
+            if len(line) + len(a) + 1 > maxwidth:
                 yield line
                 line = ''
-            line += ' ' + a
 
-    return '$' + ' \\\n'.join(lines())
+            # Append this argument to the current line, separating
+            # it by a space from the existing arguments.
+            if line:
+                line += ' ' + a
+            else:
+                line = a
+
+        yield line
+
+    return ' \\\n'.join(lines())

utils.format_cmdline misses last line
It appears that utils.format_cmdline fails to yield the last accumulated line.

This also means that a better test could be written, which splits the result back out, and compares it to the list of input arguments. 
diff --git a/tests/test_utils.py b/tests/test_utils.py
new file mode 100644
index 0000000..ff9ad97
--- /dev/null
+++ b/tests/test_utils.py
@@ -0,0 +1,49 @@
+from __future__ import print_function
+
+from nose.tools import *
+from unittest import TestCase
+
+import logging
+import shlex
+from itertools import chain
+
+from .utils import *
+
+import scuba.utils
+
+
+class TestUtils(TestCase):
+
+    def _parse_cmdline(self, cmdline):
+        # Strip the formatting and whitespace
+        lines = [l.rstrip('\\').strip() for l in cmdline.splitlines()]
+
+        # Split each line, and return a flattened list of arguments
+        return chain.from_iterable(map(shlex.split, lines))
+
+    def _test_format_cmdline(self, args):
+
+        # Call the unit-under-test to get the formatted command line
+        result = scuba.utils.format_cmdline(args)
+
+        # Parse the result back out to a list of arguments
+        out_args = self._parse_cmdline(result)
+
+        # Verify that they match
+        assert_seq_equal(out_args, args)
+
+
+    def test_basic(self):
+        '''format_cmdline works as expected'''
+
+        self._test_format_cmdline([
+            'something',
+            '-a',
+            '-b',
+            '--long', 'option text',
+            '-s', 'hort',
+            'a very long argument here that will end up on its own line because it is so wide and nothing else will fit at the default width',
+            'and now',
+            'some', 'more', 'stuff',
+            'and even more stuff',
+        ])

pip install -e .
parse_log_pytest
requirements.txt
nose pytest
apt-get update apt-get install -y gcc
3.5
requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
attrs==22.2.0
certifi==2021.5.30
coverage==3.7.1
importlib-metadata==4.8.3
iniconfig==1.1.1
nose==1.3.7
packaging==21.3
pluggy==1.0.0
py==1.11.0
pyparsing==3.1.4
pytest==7.0.1
PyYAML==6.0.1
-e git+https://github.com/JonathonReinhart/scuba.git@9aa705d7d0419f1930ae034e2210d69f66f5bf2a#egg=SCUBA
tomli==1.2.3
typing_extensions==4.1.1
zipp==3.6.0

name: scuba
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2021.5.30=py36h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.3=he6710b0_2
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=21.2.2=py36h06a4308_0
  - python=3.6.13=h12debd9_1
  - readline=8.2=h5eee18b_0
  - setuptools=58.0.4=py36h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.37.1=pyhd3eb1b0_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - attrs==22.2.0
      - coverage==3.7.1
      - importlib-metadata==4.8.3
      - iniconfig==1.1.1
      - nose==1.3.7
      - packaging==21.3
      - pluggy==1.0.0
      - py==1.11.0
      - pyparsing==3.1.4
      - pytest==7.0.1
      - pyyaml==6.0.1
      - tomli==1.2.3
      - typing-extensions==4.1.1
      - zipp==3.6.0
prefix: /opt/conda/envs/scuba

tests/test_utils.py::TestUtils::test_basic
scuba/__main__.py scuba/utils.py
scuba/__main__.py scuba/utils.py