Skip to content
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

Bash version of argparse/logging #75

Open
pfuntner opened this issue Jun 4, 2020 · 2 comments
Open

Bash version of argparse/logging #75

pfuntner opened this issue Jun 4, 2020 · 2 comments
Labels

Comments

@pfuntner
Copy link
Owner

pfuntner commented Jun 4, 2020

It would be nice to have Bash versions of the Python argparse & logging libraries. Though I don't write in Bash version very often there is sometimes a need and it could be a nice exercise anyway.

The object-oriented aspect of the use must be addressed. So instead of doing something in Python like:

import logging
import argparse
...
parser = argparse.ArgumentParser(description=DESCRIPTION_GOES_HERE_DUMMY)
parser.add_argument('-v', '--verbose', action='count', help='Enable debugging')
args = parser.parse_args()

logging.basicConfig(format='%(asctime)s %(levelname)s %(pathname)s:%(lineno)d %(msg)s')
log = logging.getLogger()
log.setLevel(logging.WARNING - (args.verbose or 0)*10)
...
log.info('This is a test')

it could be done in Bash like:

parser=$(argparse argument-parser description="DESCRIPTION_GOES_HERE_DUMMY")
parser=$(argparse add-argument --parser "$parser" -v --verbose --action=count --help="Enable debugging")
args=$(argparse parser_args --parser "$parser" "$@")

log=$(logging --basic-config --format='%(asctime)s %(levelname)s %(pathname)s:%(lineno)d %(msg)s')
log_level=$(logging --log "$log" --get-level WARNING)
verbose=$(argparse --log "$log" --get verbose)
if "X$verbose" != X
then
  let log_level="$log_level - $verbose * 10"
fi
log=$(logging --log "$log" --set-level $log_level)
...
logging --log "$log" --info "This is a test"

I'm not sure if this is possible or not but it could be fun!

@pfuntner pfuntner added the idea label Aug 13, 2022
@pfuntner
Copy link
Owner Author

pfuntner commented Mar 30, 2024

I created a new prototype repository (private... shhhhhh!) and have a solution for argparse that's pretty close to what I had in mind: https://github.com/pfuntner/proto/tree/master/argparse.

I might try a prototype of logging too.

@pfuntner
Copy link
Owner Author

logging was more simple than argparse: https://github.com/pfuntner/proto/tree/master/logging

Next I want to unite the two into a single practical example. I may be close to promoting this stuff to my toys repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant