]> git.rocketbowman.com Git - nom.git/commitdiff
add --fields flag; user-specified order
authorKyle Bowman <kyle+github@rocketbowman.com>
Mon, 27 Jan 2025 01:14:52 +0000 (20:14 -0500)
committerKyle Bowman <kyle+github@rocketbowman.com>
Mon, 27 Jan 2025 01:14:52 +0000 (20:14 -0500)
src/nom/base.py
src/nom/main.py
tests/test_cli.py

index 2dde4cbeeac389f599b8e9d7187d30f92c522f8b..582639c6a155e0c291bcbae518167ee1780e58ac 100644 (file)
@@ -29,7 +29,7 @@ class NomListItem(BaseModel):
     def to_str(self, fields: Optional[list[str]]=None, delimiter: str ='|'):
         if not fields:
             fields = [str(k) for k in self.__dict__.keys()]
-        lst = [str(v) for k,v in self.__dict__.items() if k in fields]
+        lst = [str(self.__dict__[f]) for f in fields]
         return delimiter.join(lst)
 
     def to_dict(self):
@@ -95,13 +95,13 @@ class NomList(set):
             writer.writeheader()
             for item in self:
                 writer.writerow(item.to_dict())
-    
+
     def to_stdout(self, fields=None, show_header=False, delimiter="|"):
         if not self:
             raise NomError("There are no entries to write.")
         if show_header:
-            headers = next(iter(self)).get_fieldnames()
-            print(delimiter.join(headers))
+            valid_fields = next(iter(self)).get_fieldnames()
+            header = [f for f in fields if f in valid_fields]
+            print(delimiter.join(header))
         for item in self:
-            print(item.to_str(fields=fields, delimiter=delimiter))
-    
\ No newline at end of file
+            print(item.to_str(fields=fields, delimiter=delimiter))
\ No newline at end of file
index 1fa4dc6b4b5fcd7c745202f98b9c8ff81a9806ef..d217cf237fd77e6ee715703552dcd18e9564f089 100644 (file)
@@ -30,7 +30,8 @@ def cli():
     entry_subparsers = entry_parser.add_subparsers(dest='entry_command', help='Dispatches entry commands.')
     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_show_parser.add_argument('--show-header', action='store_true', help="when specified, show the column names, not just the columns.")
+    entry_show_parser.add_argument('--field', dest="fields", nargs="*", help="specifies which columns to show")
     entry_update_parser = entry_subparsers.add_parser('update', parents=[label_parser,filter_parser], help='update entry data.')
     entry_update_parser.set_defaults(func=handle_entry_update)
     entry_update_parser.add_argument('--update', dest="updates", nargs="*", help="Update values as key=value pairs")
@@ -56,10 +57,7 @@ def handle_entry_show(args):
     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:
-        elist.to_stdout(fields=['url'])
+    elist.to_stdout(show_header=args.show_header, fields=args.fields)
 
 def handle_entry_update(args):
     if not ENTRY_LIST.exists():
index 18b14b7e5457ad172b0a17af62b048e7b0849607..6c0e15d0eb6acd47e226238eab424506c5899750 100644 (file)
@@ -19,8 +19,8 @@ def test_nom_feed_show():
 def test_nom_entry_show():
     argtest('entry show')
 
-def test_nom_entry_show_full():
-    argtest('entry show --full')
+def test_nom_entry_show_header():
+    argtest('entry show --show-header')
 
 def test_nom_entry_show_one():
     argtest('entry show 1')