commit 91bd428c22d3b608d8a40e5f86bdfa5591d69f7a
parent 1db7f3f8c85e677d0bd8d90562d92270fb43c9b5
Author: lash <dev@holbrook.no>
Date: Thu, 24 Feb 2022 15:51:06 +0000
Add formatting options for cli
Diffstat:
5 files changed, 31 insertions(+), 8 deletions(-)
diff --git 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
@@ -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
@@ -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
@@ -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
@@ -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