-
This is what I am trying to do: (more or less) from dataclasses import dataclass
from typing import Union
from simple_parsing import ArgumentParser, parse
from pathlib import Path
@dataclass
class Belch:
"""Phrases to Belch out."""
phrase: str = 'Here is some sample text' # Sample text to belch out.
times: int = int(4) # How man times to perform the belch.
def execute():
print(f'{cfg.speaker} belches:')
for _ in range(0, self.times):
print(self.phrase)
print(cfg.ending)
write1 = str(f'{cfg.speaker} belched {phrase} this many {self.times} times')
write2 = str(f'and then said {cfg.ending}')
write_this = write1 + write2
if prog.tofile:
with open(prog.txtfile, 'a') as rd:
rd.write(write_this)
rd.close
@dataclass
class Whisper:
"""Phrases to Whisper"""
words: str = 'Here is something to whisper' # Sample text to whisper.
repeat: bool = False # To repeat the whisper phrase
def execute():
opener = str(f'{cfg.speaker says:}')
print(opener)
wordsalad = str(f'(Whisper){self.words}(whisper)')
print(wordsalad)
if repeat:
print(wordsalad)
repeat_write = True
print(f'{cfg.ending}')
if repeat_write:
writetxt = opener + wordsalad + wordsalad + cfg.ending
else:
writetxt = opener + wordsalad + cfg.ending
if prog.tofile:
with open(prog.txtfile, 'a') as wf:
wf.write(writetxt)
wf.close
@dataclass
class TomlConfig:
speaker: str = 'Mr. Foo' # Person performing the belching or whispering.
ending: str = 'Thanks' # End salutation
@dataclass
class Program:
command: Union[Whisper, Belch, TomlConfig]
tofile: bool = False # Should the phrase be written to file.
txtfile: Path = Path('somefile.txt') # File to write phrase to.
ap = ArgumentParser()
ap.add_arguments(Program, dest="prog")
args = ap.parse_args()
prog: Program = args.prog
cfg = parse(
config_class=TomlConfig,
args=TomlConfig.__dataclass_fields__, # 'args' will generate error.
add_config_path_arg="config.toml",
) And here is the configuration file: speaker= "Mr.Foo"
ending= "bar is me" Whew... that was unnecessarily elaborate. Here is the error output: Error output: usage: belch.py [-h] [--config.toml Path] [--speaker str] [--ending str]
belch.py: error: unrecognized arguments: speaker ending Almost arrived at a working solution by examining the composition example. So, the final block of code that starts with def main(args=None) -> None:
plog = OtLog()
log = plog.get_log()
ap = ArgumentParser(
add_config_path_arg=True, config_path="belch.toml"
)
ap.add_arguments(Program, dest='prog')
ap.add_arguments(TomlConfig, dest='cfg')
if isinstance(args, str):
args = shlex.split(args)
args = ap.parse_args(args)
prog: Program = args.prog
cfg: TomlConfig = args.cfg
if __name__ == '__main__':
main() Now, what needs to happen is to perform the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
AND, I worked it out, finally. Dataclass Program was missing the execute function, and then all execute functions for dataclasses needed both 'args' (prog & cfg) passed back to them. So, in short, the last block looked something like this. @dataclass
class Program:
command: Union[Whisper, Belch, TomlConfig]
tofile: bool = False # Should the phrase be written to file.
txtfile: Path = Path('somefile.txt') # File to write phrase to.
def execute(self, prog, cfg):
return self.command.execute(prog, cfg)
def main(args=None) -> None:
ap = ArgumentParser(
add_config_path_arg=True, config_path="belch.toml"
)
ap.add_arguments(Program, dest='prog')
ap.add_arguments(TomlConfig, dest='cfg')
if isinstance(args, str):
args = shlex.split(args)
args = ap.parse_args(args)
prog: Program = args.prog
cfg: TomlConfig = args.cfg
prog.execute(prog, cfg)
if __name__ == '__main__':
main() |
Beta Was this translation helpful? Give feedback.
AND, I worked it out, finally. Dataclass Program was missing the execute function, and then all execute functions for dataclasses needed both 'args' (prog & cfg) passed back to them. So, in short, the last block looked something like this.