Skip to content

Slightly increase the tutorial's code snippets consistency#7319

Open
Fedeton wants to merge 2 commits into
dart-lang:mainfrom
Fedeton:tutorial-code-examples
Open

Slightly increase the tutorial's code snippets consistency#7319
Fedeton wants to merge 2 commits into
dart-lang:mainfrom
Fedeton:tutorial-code-examples

Conversation

@Fedeton
Copy link
Copy Markdown

@Fedeton Fedeton commented May 30, 2026

Tutorial code snippets a little more consistent

The changes in the commit slightly increase the consistency of the code snippets in the tutorial.

Polish your CLI app

When running cli.dart help --command <input> an exception is thrown if <input> is not a valid command as shown in the following piece of code where <input> is contained in the variable named input:

var cmd = runner.commands.firstWhere(
    (command) => command.name == input,
    orElse: () {
        throw ArgumentException(
            'Input ${args.commandArg} is not a known command.',
        );
    },
);

Here, args.commandArg is the positional argument's value for the help command, but the user wants to view the details of the command specified as value to the option --command. So, shouldn't the input variable be used instead of args.commandArg when creating the exception message?
Furthermore, the name of the running command is also passed to the constructor of the ArgumentException class.

Fetch data from the internet

In this chapter are defined the functions getRandomArticleSummary(), getArticleSummaryByTitle(), search() and getArticleByTitle(). Each function makes the proper API request and then throws an HttpException if the response's status code is not 200. This commit uniforms the exception message by using the same message prefix for all functions and by specifying the exact name of the function that is throwing the exception.

Add logging for debugging and monitoring

The first change for this chapter is within the main() function in which the onError callback is passed to the CommandRunner constructor. If the error received by this callback is an Exception instance, then it is not printed to the console anymore, it's instead just logged. This causes an ArgumentException error to not be shown as console output and the user must look into the log file. With this commit, the exception is printed again after being logged.

The second change is made in the run() method of the SearchCommand class. Now, the arguments validation will throw an ArgumentException instead of just returning a string. This behavior is consistent with that of the HelpCommand class.

The last change is also in the run() method of SearchCommand class and it adds a new line character between the string 'Search results:' and the first result element.

@google-cla
Copy link
Copy Markdown

google-cla Bot commented May 30, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates several tutorial markdown files to correct exception messages, throw ArgumentException instead of returning error strings, add a newline to search results, and print caught exceptions. Feedback recommends using simpler string interpolation ($input instead of ${input}) and adding a trailing comma in the ArgumentException instantiation in cli-polish.md.

Comment on lines +92 to +93
'Input ${input} is not a known command.',
name
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

According to the Dart style guide (Effective Dart), you should prefer interpolating pure identifiers directly without curly braces (i.e., $input instead of ${input}). Additionally, adding a trailing comma after name is recommended for consistent formatting in multi-line argument lists.

Suggested change
'Input ${input} is not a known command.',
name
'Input $input is not a known command.',
name,
References
  1. Avoid curly braces in interpolation when just an identifier. (link)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant