-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
gh-114576: Add command-line interface for dbm module #137893
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?
Conversation
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.
Thanks! Could you add dbm
to main CLI list Doc/library/cmdline.rst
?
And I think this feature deserves to be in the "What's New" entry.
|
||
|
||
.. _dbm-commandline: | ||
.. program:: dbm |
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.
.. program::
directive should be placed closer to options
.. program:: dbm |
|
||
.. option:: --whichdb file [file ...] |
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.
see above
.. option:: --whichdb file [file ...] | |
.. program:: dbm | |
.. option:: --whichdb file [file ...] |
|
||
.. option:: -h, --help | ||
|
||
Show the help message. |
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.
I'm not sure we should add help flag as option because it's not a common practice.
.. option:: -h, --help | |
Show the help message. |
Lib/dbm/__main__.py
Outdated
for filename in filenames: | ||
if os.path.exists(filename): | ||
db_type = whichdb(filename) | ||
print(f"{db_type or 'UNKNOWN'} - {filename}") |
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.
A hyphen is not needed.
Lib/dbm/__main__.py
Outdated
def _dump_command(filename): | ||
try: | ||
with dbm_open(filename, "r") as db: | ||
for key in db.keys(): |
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.
Don't use keys()
, it reads all keys in memory. Use iteration.
Lib/dbm/__main__.py
Outdated
key_str = key.decode("utf-8", errors="replace") | ||
value_str = db[key].decode("utf-8", errors="replace") |
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.
Keys and values are bytes. Such decoding may result in loss of information.
How to output bytes without loss? One way to use repr()
(not very readable for non-ASCII text). Other way to write to sys.stdout.buffer
(this will not work on Windows with not redirected stdout). There are other ways, this should be considered together with the problem of separators (see below).
group = parser.add_mutually_exclusive_group(required=True) | ||
group.add_argument( | ||
"--whichdb", |
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.
Instead of options, why not use subparsers for subcommands?
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.
I chose not to include it to maintain clarity and simplicity, but I’m happy to add it if needed.
Lib/dbm/__init__.py::__main__
as a script #114576📚 Documentation preview 📚: https://cpython-previews--137893.org.readthedocs.build/