@@ -57,7 +57,9 @@ class Flag:
5757class BoolFlag (Flag ):
5858 default : bool = False
5959
60- type FlagStore = Lit ['assign' ] | Lit ['append' ]
60+
61+ type FlagStore = Lit ['assign' ] | Lit ['append' ]
62+
6163
6264@dataclass (frozen = True )
6365class OperFlag (Flag ):
@@ -71,7 +73,6 @@ class OperFlag(Flag):
7173############
7274
7375
74-
7576class Registry :
7677 """Stores supported element types and element forms (containers)."""
7778
@@ -114,20 +115,17 @@ def _str_conv(v: str) -> str:
114115 str : _str_conv ,
115116 }
116117
117-
118118 forms : dict [str , Form ] = {
119119 'list' : Form (ctype = list , sep = ',' ),
120120 'lines' : Form (ctype = list , sep = '\n ' ),
121121 'words' : Form (ctype = list , sep = ' ' ),
122122 }
123123
124-
125124 """Builtin flags."""
126125
127126 _required_flag = BoolFlag ('required' )
128127 _sep_flag = OperFlag ('sep' , etype = str )
129128
130-
131129 flags : dict [str , BoolFlag ] = {
132130 'required' : _required_flag ,
133131 'require' : _required_flag ,
@@ -138,7 +136,6 @@ def _str_conv(v: str) -> str:
138136 'separate' : _sep_flag ,
139137 'sep' : _sep_flag ,
140138 }
141-
142139
143140
144141##########################
@@ -200,7 +197,6 @@ def __post_init__(self) -> None:
200197 if flag .name not in self .flags :
201198 self .flags [flag .name ] = flag .default
202199
203-
204200 def parse (self , rawval : str | None ) -> Value :
205201 """
206202 Parses the raw input string into the target Value.
@@ -337,8 +333,7 @@ def _apply_modifier(self, mod: str):
337333 vals .append (val )
338334 else :
339335 raise ValueError (
340- f'unsupported flag store: "{ flag .store } ". '
341- f'available: { FlagStore } '
336+ f'unsupported flag store: "{ flag .store } ". available: { FlagStore } '
342337 )
343338
344339 # Deal with builtin flags.
@@ -358,12 +353,30 @@ def _apply_modifier(self, mod: str):
358353 raise ValueError (f"unknown modifier: '{ mod } '" )
359354
360355
361- @dataclass
356+ @dataclass ( frozen = True )
362357class Schema (object ):
363358 name : Field | None
364359 attrs : dict [str , Field ] | Field
365360 content : Field | None
366361
362+ @classmethod
363+ def from_dsl (
364+ cls ,
365+ name : str | None = None ,
366+ attrs : str | dict [str , str ] = {},
367+ content = str | None ,
368+ ) -> Schema :
369+ name_field = Field .from_dsl (name ) if name else None
370+
371+ if isinstance (attrs , str ):
372+ attrs_field = Field .from_dsl (attrs )
373+ else :
374+ attrs_field = {k : Field .from_dsl (v ) for k , v in attrs .items ()}
375+
376+ cont_field = Field .from_dsl (name ) if name else None
377+
378+ return Schema (name_field , attrs_field , cont_field )
379+
367380 def _parse_single (
368381 self , field : tuple [str , Field | None ], rawval : str | None
369382 ) -> Value :
0 commit comments