]> git.rocketbowman.com Git - nom.git/commitdiff
make stdout tab-delimited by default
authorKyle Bowman <kyle+github@rocketbowman.com>
Mon, 27 Jan 2025 01:36:05 +0000 (20:36 -0500)
committerKyle Bowman <kyle+github@rocketbowman.com>
Mon, 27 Jan 2025 01:36:05 +0000 (20:36 -0500)
src/nom/base.py
src/nom/main.py

index 582639c6a155e0c291bcbae518167ee1780e58ac..8f8ec727ffafcd5570af8e9040628b7f5a1e3099 100644 (file)
@@ -26,7 +26,7 @@ class NomListItem(BaseModel):
         self.__setattr__(key, val)
     
     # TODO: What if there's a pipe in one of the fields? 
-    def to_str(self, fields: Optional[list[str]]=None, delimiter: str ='|'):
+    def to_str(self, fields: Optional[list[str]]=None, delimiter: str ='\t'):
         if not fields:
             fields = [str(k) for k in self.__dict__.keys()]
         lst = [str(self.__dict__[f]) for f in fields]
@@ -96,12 +96,15 @@ class NomList(set):
             for item in self:
                 writer.writerow(item.to_dict())
 
-    def to_stdout(self, fields=None, show_header=False, delimiter="|"):
+    def to_stdout(self, fields=None, show_header=False, delimiter="\t"):
         if not self:
             raise NomError("There are no entries to write.")
         if show_header:
             valid_fields = next(iter(self)).get_fieldnames()
-            header = [f for f in fields if f in valid_fields]
+            if fields: 
+                header = [f for f in fields if f in valid_fields]
+            else: 
+                header = valid_fields
             print(delimiter.join(header))
         for item in self:
             print(item.to_str(fields=fields, delimiter=delimiter))
\ No newline at end of file
index d217cf237fd77e6ee715703552dcd18e9564f089..2e3744822e524ac27f1b7fdb88b450200259bcb7 100644 (file)
@@ -31,6 +31,7 @@ def cli():
     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('--show-header', action='store_true', help="when specified, show the column names, not just the columns.")
+    entry_show_parser.add_argument('--del', dest="delim",help="specifies the field delimiter; default tab")
     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)
@@ -57,7 +58,9 @@ def handle_entry_show(args):
     if args.select:
         [k, v] = args.select.split('=')
         elist = elist.select(lambda e: str(e.__dict__[k]) == v)
-    elist.to_stdout(show_header=args.show_header, fields=args.fields)
+    if not args.delim:
+        args.delim = "\t"
+    elist.to_stdout(show_header=args.show_header, fields=args.fields, delimiter=args.delim)
 
 def handle_entry_update(args):
     if not ENTRY_LIST.exists():