Add formatting options for cli

This commit is contained in:
lash 2022-02-24 15:51:06 +00:00
parent 1db7f3f8c8
commit 91bd428c22
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 31 additions and 8 deletions

View File

@ -5,6 +5,7 @@ from .base import (
argflag_std_base, argflag_std_base,
argflag_std_base_read, argflag_std_base_read,
flag_reset, flag_reset,
flag_set,
) )
from .arg import ArgumentParser from .arg import ArgumentParser
from .config import Config from .config import Config

View File

@ -43,6 +43,10 @@ _default_dest = {
'-e': 'executable_address', '-e': 'executable_address',
} }
_default_fmt = 'human'
class ArgumentParser(argparse.ArgumentParser): class ArgumentParser(argparse.ArgumentParser):
"""Extends the standard library argument parser to construct arguments based on configuration flags. """Extends the standard library argument parser to construct arguments based on configuration flags.
@ -66,12 +70,13 @@ class ArgumentParser(argparse.ArgumentParser):
:type epilog: str :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) super(ArgumentParser, self).__init__(usage=usage, description=description, epilog=epilog, formatter_class=argparse.RawDescriptionHelpFormatter, *args, **kwargs)
self.pos_args = [] self.pos_args = []
self.long_args = _default_long_args self.long_args = _default_long_args
self.arg_dest = _default_dest self.arg_dest = _default_dest
self.default_format = default_format
re_long = r'^--[a-z\-]+$' re_long = r'^--[a-z\-]+$'
for k in arg_long.keys(): 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') self.add_argument('-e', self.long_args['-e'], dest=self.arg_dest['-e'], type=str, help='contract address')
if arg_flags & Flag.WALLET: if arg_flags & Flag.WALLET:
self.add_argument('-a', self.long_args['-a'], dest=self.arg_dest['-a'], type=str, help='recipient address') 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))

View File

@ -33,6 +33,10 @@ class Flag(enum.IntEnum):
SEND = 262144 SEND = 262144
# rpc extras - nibble 6 # rpc extras - nibble 6
RPC_AUTH = 1048576 RPC_AUTH = 1048576
# formatting - nibble 7
FMT_HUMAN = 16777216
FMT_WIRE = 33554432
FMT_RPC = 67108864
# upper bound # upper bound
MAX = 1048576 MAX = 1048576
@ -40,8 +44,8 @@ argflag_std_read = 0x23ff
argflag_std_write = 0x1731ff argflag_std_write = 0x1731ff
argflag_std_base = 0x200f argflag_std_base = 0x200f
argflag_std_base_read = 0xbf argflag_std_base_read = 0xbf
argflag_std_target = 0x00e000 argflag_std_target = 0x0000e000
argflag_all = 0x17f7ff argflag_all = 0x0317f7ff
def flag_reset(flags, v): def flag_reset(flags, v):
@ -50,6 +54,10 @@ def flag_reset(flags, v):
return r return r
def flag_set(flags, v):
return flags | v
def flag_names(flags): def flag_names(flags):
flags_debug = [] flags_debug = []
i = Flag.MAX i = Flag.MAX

View File

@ -205,7 +205,7 @@ class Config(confini.Config):
config.add(getattr(args, 'raw'), '_RAW') config.add(getattr(args, 'raw'), '_RAW')
args_override = {} args_override = {}
if arg_flags & Flag.PROVIDER: if arg_flags & Flag.PROVIDER:
args_override['RPC_PROVIDER'] = getattr(args, 'p') args_override['RPC_PROVIDER'] = getattr(args, 'p')
args_override['RPC_DIALECT'] = getattr(args, 'rpc_dialect') args_override['RPC_DIALECT'] = getattr(args, 'rpc_dialect')
@ -222,7 +222,7 @@ class Config(confini.Config):
args_override['WALLET_PASSPHRASE'] = f.read() args_override['WALLET_PASSPHRASE'] = f.read()
f.close() f.close()
config.censor('PASSPHRASE', 'WALLET') 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: if arg_flags & Flag.PROVIDER:
config.add(getattr(args, 'height'), '_HEIGHT') config.add(getattr(args, 'height'), '_HEIGHT')

View File

@ -1,4 +1,4 @@
funga~=0.5.1 funga~=0.5.2
pysha3==1.0.2 pysha3==1.0.2
hexathon~=0.1.3 hexathon~=0.1.5
confini~=0.5.3 confini~=0.5.7