From: Kyle Bowman Date: Mon, 27 Jan 2025 00:09:00 +0000 (-0500) Subject: add --select field=value functionality to show X-Git-Url: https://git.rocketbowman.com/?a=commitdiff_plain;h=84e7846662b24051b72744ae37717326aab98200;p=nom.git add --select field=value functionality to show --- diff --git a/src/nom/base.py b/src/nom/base.py index 4d0a78c..2dde4cb 100644 --- a/src/nom/base.py +++ b/src/nom/base.py @@ -69,10 +69,6 @@ class NomList(set): items = {item for item in self if predicate(item)} return self.__class__(items) - #def to_stdout(self, fields=[]): - # for item in self.items: - # print(item.to_str(fields=fields, delimiter="\t")) - @classmethod def from_csv(cls, file: Path, delimiter="|"): items = [] diff --git a/src/nom/main.py b/src/nom/main.py index 3336fd1..bad89e6 100644 --- a/src/nom/main.py +++ b/src/nom/main.py @@ -16,6 +16,10 @@ def cli(): # Label Parser label_parser = ArgumentParser(add_help=False) label_parser.add_argument('label', nargs='*', type=int) + + # Filter Parser + filter_parser = ArgumentParser(add_help=False) + filter_parser.add_argument("--select", type =str, help="specifies a field=value criteria to select by") # Main Parser parser = ArgumentParser(description="Nom Script") @@ -24,7 +28,7 @@ def cli(): # Entry subcommand entry_parser = subparsers.add_parser('entry', help='Dispatches commands that operate on a table of entries.') entry_subparsers = entry_parser.add_subparsers(dest='entry_command', help='Dispatches entry commands.') - entry_show_parser = entry_subparsers.add_parser('show', parents=[label_parser], help='Show entries.') + entry_show_parser = entry_subparsers.add_parser('show', parents=[label_parser,filter_parser], help='Show entries.') entry_show_parser.set_defaults(func=handle_entry_show) entry_show_parser.add_argument('--full', action='store_true', help="when specified, show the full entry data, not just the URLs.") entry_update_parser = entry_subparsers.add_parser('update', parents=[label_parser], help='update entry data.') @@ -49,6 +53,9 @@ def handle_entry_show(args): elist = EntryList.from_csv(ENTRY_LIST) if args.label: elist = elist.select(lambda e: e.label in args.label) + if args.select: + [k, v] = args.select.split('=') + elist = elist.select(lambda e: str(e.__dict__[k]) == v) if args.full: elist.to_stdout(show_header=True) else: diff --git a/tests/test_cli.py b/tests/test_cli.py index 556fb1a..18b14b7 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -25,6 +25,9 @@ def test_nom_entry_show_full(): def test_nom_entry_show_one(): argtest('entry show 1') +def test_nom_entry_show_unviewed(): + argtest('entry show --select viewed=False') + def test_nom_entry_update_no_elist(tmp_path): ENTRY_LIST = tmp_path / "entry_list" / "default" argtest('entry update')