pfuntner / toys

Tools and things that make my life easier - y'all might like them too
11 stars 2 forks source link

Bash version of argparse/logging #75

Open pfuntner opened 4 years ago

pfuntner commented 4 years ago

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 commented 7 months ago

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 commented 7 months ago

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.