Skip to content

Commit f2cb3a0

Browse files
committed
Changed default value for USE_ARG_LIST global to True
Now by default all @options commands get passed a list of argument strings instead of a single argument string. This is a much easier and more robust behavior to deal with. Additionally, command-line arguments are intelligently separated based on location of quotes to group things into a single argument. WARNING: This change breaks backward compatibility for older applicaitons based on cmd2. To change the behavior to the way it used to be, add the following code to the __init__() method of our class derived from cmd2.Cmd: cmd2.set_use_arg_list(False) This change really does make it easier for developers new to using cmd2 however. It is to the point where I create all custom commands with @options, even if I use an empty list for the options because the argument parsing is just much better this way.
1 parent 056ea31 commit f2cb3a0

File tree

5 files changed

+16
-12
lines changed

5 files changed

+16
-12
lines changed

cmd2.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@
9797

9898

9999
# The next 3 variables and associated setter functions effect how arguments are parsed for commands using @options.
100-
# The defaults are "sane" and maximize backward compatibility with cmd and previous versions of cmd2.
101-
# But depending on your particular application, you may wish to tweak them so you get the desired parsing behavior.
100+
# The defaults are "sane" and maximize ease of use for new applications based on cmd2.
101+
# To maximize backwards compatibility, we recommend setting USE_ARG_LIST to "False"
102102

103103
# Use POSIX or Non-POSIX (Windows) rules for splitting a command-line string into a list of arguments via shlex.split()
104104
POSIX_SHLEX = False
@@ -107,7 +107,7 @@
107107
STRIP_QUOTES_FOR_NON_POSIX = True
108108

109109
# For option commands, pass a list of argument strings instead of a single argument string to the do_* methods
110-
USE_ARG_LIST = False
110+
USE_ARG_LIST = True
111111

112112

113113
def set_posix_shlex(val):

examples/example.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
verifying that the output produced matches the transcript.
1010
"""
1111

12-
from cmd2 import Cmd, make_option, options
12+
from cmd2 import Cmd, make_option, options, set_use_arg_list
1313

1414

1515
class CmdLineApp(Cmd):
@@ -26,6 +26,9 @@ def __init__(self):
2626
# Set use_ipython to True to enable the "ipy" command which embeds and interactive IPython shell
2727
Cmd.__init__(self, use_ipython=False)
2828

29+
# For option commands, pass a single argument string instead of a list of argument strings to the do_* methods
30+
set_use_arg_list(False)
31+
2932
@options([make_option('-p', '--piglatin', action="store_true", help="atinLay"),
3033
make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"),
3134
make_option('-r', '--repeat', type="int", help="output [n] times")

examples/python_scripting.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919

2020
from cmd2 import Cmd, options, make_option, CmdResult, set_use_arg_list
2121

22-
# For option commands, pass a list of argument strings instead of a single argument string to the do_* methods
23-
set_use_arg_list(True)
24-
2522

2623
class CmdLineApp(Cmd):
2724
""" Example cmd2 application to showcase conditional control flow in Python scripting within cmd2 aps. """
@@ -33,6 +30,9 @@ def __init__(self):
3330
self.autorun_on_edit = False
3431
self.intro = 'Happy 𝛑 Day. Note the full Unicode support: 😇 (Python 3 only) 💩'
3532

33+
# For option commands, pass a list of argument strings instead of a single argument string to the do_* methods
34+
set_use_arg_list(True)
35+
3636
def _set_prompt(self):
3737
"""Set prompt so it displays the current working directory."""
3838
self.cwd = os.getcwd()

tests/test_cmd2.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,11 @@ def test_base_cmdenvironment(base_app):
227227
Command-line arguments allowed: True
228228
Output redirection and pipes allowed: True
229229
Parsing of @options commands:
230-
Use POSIX-style argument parser (vs Windows): False
231-
Strip Quotes when using Windows-style argument parser: True
232-
Use a list of arguments instead of a single argument string: False
230+
Use POSIX-style argument parser (vs Windows): {}
231+
Strip Quotes when using Windows-style argument parser: {}
232+
Use a list of arguments instead of a single argument string: {}
233233
234-
""")
234+
""".format(cmd2.POSIX_SHLEX, cmd2.STRIP_QUOTES_FOR_NON_POSIX, cmd2.USE_ARG_LIST))
235235
assert out == expected
236236

237237
def test_base_load(base_app, request):

tests/test_transcript.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# Used for sm.input: raw_input() for Python 2 or input() for Python 3
1616
import six.moves as sm
1717

18-
from cmd2 import Cmd, make_option, options, Cmd2TestCase
18+
from cmd2 import Cmd, make_option, options, Cmd2TestCase, set_use_arg_list
1919
from conftest import run_cmd, StdOut, normalize
2020

2121

@@ -28,6 +28,7 @@ def __init__(self, *args, **kwargs):
2828
# Need to use this older form of invoking super class constructor to support Python 2.x and Python 3.x
2929
Cmd.__init__(self, *args, **kwargs)
3030
self.settable.append('maxrepeats Max number of `--repeat`s allowed')
31+
set_use_arg_list(False)
3132

3233
opts = [make_option('-p', '--piglatin', action="store_true", help="atinLay"),
3334
make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"),

0 commit comments

Comments
 (0)