Skip to content

Commit 01d1ab6

Browse files
committed
Remove the dirty arg management, only keep argparse
fix nojhan#70
1 parent fb9de00 commit 01d1ab6

File tree

1 file changed

+5
-71
lines changed

1 file changed

+5
-71
lines changed

colout/colout.py

Lines changed: 5 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import string
1818
import hashlib
1919
import functools
20+
import argparse
2021

2122
# set the SIGPIPE handler to kill the program instead of
2223
# ending in a write error when a broken pipe occurs
@@ -755,64 +756,7 @@ def colorgen(stream, pattern, color="red", style="normal", on_groups=False):
755756
# Command line tools #
756757
######################
757758

758-
def __args_dirty__(argv, usage=""):
759-
"""
760-
Roughly extract options from the command line arguments.
761-
To be used only when argparse is not available.
762-
763-
Returns a tuple of (pattern,color,style,on_stderr).
764-
765-
>>> colout.__args_dirty__(["colout","pattern"],"usage")
766-
('pattern', 'red', 'normal', False)
767-
>>> colout.__args_dirty__(["colout","pattern","colors","styles"],"usage")
768-
('pattern', 'colors', 'styles', False)
769-
>>> colout.__args_dirty__(["colout","pattern","colors","styles","True"],"usage")
770-
('pattern', 'colors', 'styles', True)
771-
"""
772-
773-
# Use a dirty argument picker
774-
# Check for bad usage or an help flag
775-
if len(argv) < 2 \
776-
or len(argv) > 10 \
777-
or argv[1] == "--help" \
778-
or argv[1] == "-h":
779-
print(usage+"\n")
780-
print("Usage:", argv[0], "<pattern> <color(s)> [<style(s)>] [<print on stderr?>] [<iterate over groups?>]")
781-
print("\tAvailable colors:", " ".join(context["colors"]))
782-
print("\tAvailable styles:", " ".join(context["styles"]))
783-
print("Example:", argv[0], "'^(def)\s+(\w*).*$' blue,magenta italic,bold < colout.py")
784-
sys.exit(1)
785-
786-
assert(len(argv) >= 2)
787-
# Get mandatory arguments
788-
pattern = argv[1]
789-
790-
# default values for optional args
791-
color = "red"
792-
style = "normal"
793-
on_stderr = False
794-
795-
if len(argv) >= 3:
796-
color = argv[2]
797-
if len(argv) >= 4:
798-
style = argv[3]
799-
if len(argv) == 5:
800-
on_groups = bool(argv[4])
801-
if len(argv) == 6:
802-
as_colormap = bool(argv[5])
803-
if len(argv) == 7:
804-
as_theme = bool(argv[6])
805-
if len(argv) == 8:
806-
as_source = bool(argv[7])
807-
if len(argv) == 9:
808-
as_all = bool(argv[8])
809-
if len(argv) == 10:
810-
scale = bool(argv[9])
811-
812-
return pattern, color, style, on_groups, as_colormap, as_theme, as_source, as_all, scale
813-
814-
815-
def __args_parse__(argv, usage=""):
759+
def _args_parse(argv, usage=""):
816760
"""
817761
Parse command line arguments with the argparse library.
818762
Returns a tuple of (pattern,color,style,on_stderr).
@@ -923,19 +867,9 @@ def write_all( as_all, stream_in, stream_out, function, *args ):
923867
#####################
924868
# Arguments parsing #
925869
#####################
926-
try:
927-
import argparse
928-
929-
# if argparse is not installed
930-
except ImportError:
931-
pattern, color, style, on_groups, as_colormap, as_theme, as_source, as_all, myscale \
932-
= __args_dirty__(sys.argv, usage)
933-
934-
# if argparse is available
935-
else:
936-
pattern, color, style, on_groups, as_colormap, as_theme, as_source, as_all, myscale, \
937-
debug, resources, palettes_dirs, themes_dirs, default_colormap \
938-
= __args_parse__(sys.argv, usage)
870+
pattern, color, style, on_groups, as_colormap, as_theme, as_source, as_all, myscale, \
871+
debug, resources, palettes_dirs, themes_dirs, default_colormap \
872+
= _args_parse(sys.argv, usage)
939873

940874
if debug:
941875
lvl = logging.DEBUG

0 commit comments

Comments
 (0)