Skip to content

Commit 5765bb4

Browse files
committed
Let ruff format code in docstrings the same way it formats our code
1 parent 3d75fea commit 5765bb4

5 files changed

Lines changed: 30 additions & 28 deletions

File tree

cmd2/annotated.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def do_greet(self, name: str, count: int = 1, loud: bool = False):
3232
3333
from typing import Annotated
3434
35+
3536
class MyApp(cmd2.Cmd):
3637
def color_choices(self) -> cmd2.Choices:
3738
return cmd2.Choices.from_values(["red", "green", "blue"])
@@ -40,9 +41,7 @@ def color_choices(self) -> cmd2.Choices:
4041
def do_paint(
4142
self,
4243
item: str,
43-
color: Annotated[str, Option("--color", "-c",
44-
choices_provider=color_choices,
45-
help_text="Color to use")] = "blue",
44+
color: Annotated[str, Option("--color", "-c", choices_provider=color_choices, help_text="Color to use")] = "blue",
4645
):
4746
self.poutput(f"Painting {item} {color}")
4847

cmd2/argparse_utils.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
Example::
1717
1818
# -f argument expects at least 3 values
19-
parser.add_argument('-f', nargs=(3,))
19+
parser.add_argument("-f", nargs=(3,))
2020
2121
# -f argument expects 3 to 5 values
22-
parser.add_argument('-f', nargs=(3, 5))
22+
parser.add_argument("-f", nargs=(3, 5))
2323
2424
2525
**Completion**
@@ -38,8 +38,8 @@
3838
3939
Example::
4040
41-
my_list = ['An Option', 'SomeOtherOption']
42-
parser.add_argument('-o', '--options', choices=my_list)
41+
my_list = ["An Option", "SomeOtherOption"]
42+
parser.add_argument("-o", "--options", choices=my_list)
4343
4444
``choices_provider`` - pass a function that returns a Choices object. This is good in
4545
cases where the choices are dynamically generated when the user hits tab.
@@ -50,6 +50,7 @@ def my_choices_provider(self) -> Choices:
5050
...
5151
return my_choices
5252
53+
5354
parser.add_argument("arg", choices_provider=my_choices_provider)
5455
5556
``completer`` - pass a function that does custom completion and returns a Completions object.
@@ -60,17 +61,16 @@ def my_choices_provider(self) -> Choices:
6061
Example::
6162
6263
# This adds file-path completion to an argument
63-
parser.add_argument('-o', '--options', completer=cmd2.Cmd.path_complete)
64+
parser.add_argument("-o", "--options", completer=cmd2.Cmd.path_complete)
6465
6566
You can use functools.partial() to prepopulate values of the underlying
6667
choices and completer functions/methods.
6768
6869
Example::
6970
7071
# This says to call path_complete with a preset value for its path_filter argument
71-
dir_completer = functools.partial(path_complete,
72-
path_filter=lambda path: os.path.isdir(path))
73-
parser.add_argument('-o', '--options', completer=dir_completer)
72+
dir_completer = functools.partial(path_complete, path_filter=lambda path: os.path.isdir(path))
73+
parser.add_argument("-o", "--options", completer=dir_completer)
7474
7575
For ``choices_provider`` and ``completer``, do not set them to a bound method. This
7676
is because ArgparseCompleter passes the `self` argument explicitly to these

cmd2/decorators.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def with_category(
5252
Example:
5353
```py
5454
class MyApp(cmd2.Cmd):
55-
@cmd2.with_category('Text Functions')
55+
@cmd2.with_category("Text Functions")
5656
def do_echo(self, args: cmd2.Statement) -> None:
5757
self.poutput(args)
5858
```
@@ -159,12 +159,12 @@ class MyApp(cmd2.Cmd):
159159
# Basic usage: receives a list of words with quotes stripped
160160
@cmd2.with_argument_list
161161
def do_echo(self, arglist: list[str]) -> None:
162-
self.poutput(' '.join(arglist))
162+
self.poutput(" ".join(arglist))
163163
164164
# Factory usage: preserves quotes in the argument list
165165
@cmd2.with_argument_list(preserve_quotes=True)
166166
def do_print_raw(self, arglist: list[str]) -> None:
167-
self.poutput(' '.join(arglist))
167+
self.poutput(" ".join(arglist))
168168
```
169169
170170
"""
@@ -268,32 +268,34 @@ def with_argparser(
268268
Example:
269269
```py
270270
parser = cmd2.Cmd2ArgumentParser()
271-
parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
272-
parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
273-
parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
274-
parser.add_argument('words', nargs='+', help='words to print')
271+
parser.add_argument("-p", "--piglatin", action="store_true", help="atinLay")
272+
parser.add_argument("-s", "--shout", action="store_true", help="N00B EMULATION MODE")
273+
parser.add_argument("-r", "--repeat", type=int, help="output [n] times")
274+
parser.add_argument("words", nargs="+", help="words to print")
275+
275276
276277
class MyApp(cmd2.Cmd):
277278
@cmd2.with_argparser(parser, preserve_quotes=True)
278279
def do_argprint(self, args: argparse.Namespace) -> None:
279280
"Print the options and argument list this options command was called with."
280-
self.poutput(f'args: {args!r}')
281+
self.poutput(f"args: {args!r}")
281282
```
282283
283284
Example with unknown args:
284285
285286
```py
286287
parser = cmd2.Cmd2ArgumentParser()
287-
parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
288-
parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
289-
parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
288+
parser.add_argument("-p", "--piglatin", action="store_true", help="atinLay")
289+
parser.add_argument("-s", "--shout", action="store_true", help="N00B EMULATION MODE")
290+
parser.add_argument("-r", "--repeat", type=int, help="output [n] times")
291+
290292
291293
class MyApp(cmd2.Cmd):
292294
@cmd2.with_argparser(parser, with_unknown_args=True)
293295
def do_argprint(self, args: argparse.Namespace, unknown_args: list[str]):
294296
"Print the options and argument list this options command was called with."
295-
self.poutput(f'args: {args!r}')
296-
self.poutput(f'unknown_args: {unknown_args}')
297+
self.poutput(f"args: {args!r}")
298+
self.poutput(f"unknown_args: {unknown_args}")
297299
```
298300
299301
"""
@@ -447,14 +449,15 @@ def as_subcommand_to(
447449
base_parser.add_subparsers(title="subcommands", metavar="SUBCOMMAND", required=True)
448450
sub_parser = cmd2.Cmd2ArgumentParser()
449451
452+
450453
class MyApp(cmd2.Cmd):
451454
@cmd2.with_argparser(base_parser)
452455
def do_base(self, args: argparse.Namespace) -> None:
453456
args.cmd2_subcmd_handler(args)
454457
455-
@cmd2.as_subcommand_to('base', 'sub', sub_parser, help="the subcommand")
458+
@cmd2.as_subcommand_to("base", "sub", sub_parser, help="the subcommand")
456459
def sub_handler(self, args: argparse.Namespace) -> None:
457-
self.poutput('Subcommand executed')
460+
self.poutput("Subcommand executed")
458461
```
459462
460463
"""

cmd2/parsing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ def is_valid_command(self, word: str, *, is_subcommand: bool = False) -> tuple[b
356356
If word is not a valid command, return ``False`` and an error string
357357
suitable for inclusion in an error message of your choice::
358358
359-
checkit = '>'
359+
checkit = ">"
360360
valid, errmsg = statement_parser.is_valid_command(checkit)
361361
if not valid:
362362
errmsg = f"alias: {errmsg}"

ruff.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ line-ending = "auto"
179179
#
180180
# This is currently disabled by default, but it is planned for this
181181
# to be opt-out in the future.
182-
docstring-code-format = false
182+
docstring-code-format = true
183183

184184
# Set the line length limit used when formatting code snippets in
185185
# docstrings.

0 commit comments

Comments
 (0)