Repository navigation
Implement basic functionality for cat command with line numbering
#19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| """Concatenate files, optionally numbering their lines.""" | ||
|
|
||
| import argparse | ||
| from pathlib import Path | ||
| import sys | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument("-n", action="store_true", help="number every line") | ||
| parser.add_argument("-b", action="store_true", help="number nonempty lines") | ||
| parser.add_argument("files", nargs="+") | ||
| args = parser.parse_args() | ||
| status = 0 | ||
|
|
||
| for filename in args.files: | ||
| line_number = 1 | ||
| try: | ||
| with Path(filename).open(encoding="utf-8", newline="") as source: | ||
| for line in source: | ||
| number_line = args.n or (args.b and bool(line.strip())) | ||
| if number_line: | ||
| sys.stdout.write(f"{line_number:6}\t") | ||
| line_number += 1 | ||
|
Comment on lines
+16
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In your current implementation, On both common Would you consider where To "like" or "dislike" this comment, please follow this link |
||
| sys.stdout.write(line) | ||
| except (OSError, UnicodeError) as error: | ||
| print(f"cat: {filename}: {error}", file=sys.stderr) | ||
| status = 1 | ||
|
|
||
| return status | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The module docstring on line 1 explains what the script does at a high level. In many small scripts, the combination of the filename (
cat.py), the function/argument names, and the argument help texts already make the intent very clear. In those cases, an extra docstring can end up repeating information without adding much new context.It might be worth asking yourself: Does this comment tell a future reader anything they couldn’t already infer from the code and CLI help? If the answer is no, you could either drop the docstring or expand it to include non-obvious behavior (for example, how
-nand-binteract, or how errors are handled). That way, any comment you keep is clearly pulling its weight.To "like" or "dislike" this comment, please follow this link