From 91bd428c22d3b608d8a40e5f86bdfa5591d69f7a Mon Sep 17 00:00:00 2001 From: lash Date: Thu, 24 Feb 2022 15:51:06 +0000 Subject: [PATCH] Add formatting options for cli --- chainlib/cli/__init__.py | 1 + chainlib/cli/arg.py | 16 +++++++++++++++- chainlib/cli/base.py | 12 ++++++++++-- chainlib/cli/config.py | 4 ++-- requirements.txt | 6 +++--- 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/chainlib/cli/__init__.py b/chainlib/cli/__init__.py index 9c0b73c..31c4023 100644 --- a/chainlib/cli/__init__.py +++ b/chainlib/cli/__init__.py @@ -5,6 +5,7 @@ from .base import ( argflag_std_base, argflag_std_base_read, flag_reset, + flag_set, ) from .arg import ArgumentParser from .config import Config diff --git a/chainlib/cli/arg.py b/chainlib/cli/arg.py index 877bfa0..c140b15 100644 --- a/chainlib/cli/arg.py +++ b/chainlib/cli/arg.py @@ -43,6 +43,10 @@ _default_dest = { '-e': 'executable_address', } + +_default_fmt = 'human' + + class ArgumentParser(argparse.ArgumentParser): """Extends the standard library argument parser to construct arguments based on configuration flags. @@ -66,12 +70,13 @@ class ArgumentParser(argparse.ArgumentParser): :type epilog: str """ - def __init__(self, arg_flags=0x0f, arg_long={}, env=os.environ, usage=None, description=None, epilog=None, *args, **kwargs): + def __init__(self, arg_flags=0x0f, arg_long={}, env=os.environ, usage=None, description=None, epilog=None, default_format=_default_fmt, *args, **kwargs): super(ArgumentParser, self).__init__(usage=usage, description=description, epilog=epilog, formatter_class=argparse.RawDescriptionHelpFormatter, *args, **kwargs) self.pos_args = [] self.long_args = _default_long_args self.arg_dest = _default_dest + self.default_format = default_format re_long = r'^--[a-z\-]+$' for k in arg_long.keys(): @@ -210,3 +215,12 @@ class ArgumentParser(argparse.ArgumentParser): self.add_argument('-e', self.long_args['-e'], dest=self.arg_dest['-e'], type=str, help='contract address') if arg_flags & Flag.WALLET: self.add_argument('-a', self.long_args['-a'], dest=self.arg_dest['-a'], type=str, help='recipient address') + if arg_flags & (Flag.FMT_HUMAN | Flag.FMT_WIRE | Flag.FMT_RPC): + format_choices = [] + if arg_flags & Flag.FMT_HUMAN: + format_choices.append('human') + if arg_flags & Flag.FMT_WIRE: + format_choices.append('bin') + if arg_flags & Flag.FMT_RPC: + format_choices.append('rpc') + self.add_argument('-f', '--format', type=str, choices=format_choices, help='output formatting (default: {})'.format(self.default_format)) diff --git a/chainlib/cli/base.py b/chainlib/cli/base.py index 68c31b3..9c05995 100644 --- a/chainlib/cli/base.py +++ b/chainlib/cli/base.py @@ -33,6 +33,10 @@ class Flag(enum.IntEnum): SEND = 262144 # rpc extras - nibble 6 RPC_AUTH = 1048576 + # formatting - nibble 7 + FMT_HUMAN = 16777216 + FMT_WIRE = 33554432 + FMT_RPC = 67108864 # upper bound MAX = 1048576 @@ -40,8 +44,8 @@ argflag_std_read = 0x23ff argflag_std_write = 0x1731ff argflag_std_base = 0x200f argflag_std_base_read = 0xbf -argflag_std_target = 0x00e000 -argflag_all = 0x17f7ff +argflag_std_target = 0x0000e000 +argflag_all = 0x0317f7ff def flag_reset(flags, v): @@ -50,6 +54,10 @@ def flag_reset(flags, v): return r +def flag_set(flags, v): + return flags | v + + def flag_names(flags): flags_debug = [] i = Flag.MAX diff --git a/chainlib/cli/config.py b/chainlib/cli/config.py index d82381c..430a588 100644 --- a/chainlib/cli/config.py +++ b/chainlib/cli/config.py @@ -205,7 +205,7 @@ class Config(confini.Config): config.add(getattr(args, 'raw'), '_RAW') args_override = {} - + if arg_flags & Flag.PROVIDER: args_override['RPC_PROVIDER'] = getattr(args, 'p') args_override['RPC_DIALECT'] = getattr(args, 'rpc_dialect') @@ -222,7 +222,7 @@ class Config(confini.Config): args_override['WALLET_PASSPHRASE'] = f.read() f.close() config.censor('PASSPHRASE', 'WALLET') - config.dict_override(args_override, 'cli args') + config.dict_override(args_override, 'cli args', allow_empty=True) if arg_flags & Flag.PROVIDER: config.add(getattr(args, 'height'), '_HEIGHT') diff --git a/requirements.txt b/requirements.txt index 52353c0..44f3f5c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -funga~=0.5.1 +funga~=0.5.2 pysha3==1.0.2 -hexathon~=0.1.3 -confini~=0.5.3 +hexathon~=0.1.5 +confini~=0.5.7