11# SPDX-License-Identifier: MIT OR Apache-2.0
22# SPDX-FileCopyrightText: The Ferrocene Developers
33
4+
5+ from dataclasses import dataclass
6+ import re
7+ import string
8+
49from docutils import nodes
10+ from docutils .parsers .rst import directives
11+
512from sphinx import addnodes
613from sphinx .directives import SphinxDirective , ObjectDescription
714from sphinx .domains import Domain , ObjType
815from sphinx .roles import XRefRole
9- import re
1016import sphinx
11- import string
1217
1318
1419PROGRAM_STORAGE = "ferrocene_domain_cli:program"
@@ -18,17 +23,29 @@ class ProgramDirective(SphinxDirective):
1823 has_content = True
1924 required_arguments = 1
2025 final_argument_whitespace = True
26+ option_spec = {"no_traceability_matrix" : directives .flag }
2127
2228 def run (self ):
29+ # if there already is program data in storage, a ProgramDirective is
30+ # within a ProgramDirective, which isn't supported
2331 if PROGRAM_STORAGE in self .env .temp_data :
2432 warn ("cli:program inside cli:program isn't supported" , self .get_location ())
2533 return []
26- self .env .temp_data [PROGRAM_STORAGE ] = self .arguments [0 ]
2734
35+ # store arguments, so they can be accessed by child `OptionDirective`s
36+ self .env .temp_data [PROGRAM_STORAGE ] = ProgramStorage (
37+ self .arguments [0 ],
38+ "no_traceability_matrix" in self .options ,
39+ )
40+
41+ # parse and process content of `ProgramDirective``
42+ # (one or more `OptionDirective`s)
2843 node = nodes .container ()
2944 self .state .nested_parse (self .content , self .content_offset , node )
3045
46+ # clear program storage
3147 del self .env .temp_data [PROGRAM_STORAGE ]
48+
3249 return [node ]
3350
3451
@@ -43,11 +60,16 @@ def handle_signature(self, sig, signode):
4360 def add_target_and_index (self , name_cls , sig , signode ):
4461 if PROGRAM_STORAGE not in self .env .temp_data :
4562 warn ("cli:option outside cli:program isn't supported" , self .get_location ())
46- program = "PLACEHOLDER"
63+ program_storage = ProgramStorage ( "PLACEHOLDER" , False )
4764 else :
48- program = self .env .temp_data [PROGRAM_STORAGE ]
65+ program_storage : ProgramStorage = self .env .temp_data [PROGRAM_STORAGE ]
4966
50- option = Option (self .env .docname , program , sig )
67+ option = Option (
68+ self .env .docname ,
69+ program_storage .program_name ,
70+ sig ,
71+ program_storage .no_traceability_matrix ,
72+ )
5173
5274 signode ["ids" ].append (option .id ())
5375
@@ -61,10 +83,11 @@ def add_target_and_index(self, name_cls, sig, signode):
6183
6284
6385class Option :
64- def __init__ (self , document , program , option ):
86+ def __init__ (self , document , program , option , no_traceability_matrix ):
6587 self .document = document
6688 self .program = program
6789 self .option = option
90+ self .no_traceability_matrix = no_traceability_matrix
6891
6992 def id (self ):
7093 option = (
@@ -149,3 +172,9 @@ def warn(message, location):
149172
150173def setup (app ):
151174 app .add_domain (CliDomain )
175+
176+
177+ @dataclass
178+ class ProgramStorage :
179+ program_name : str
180+ no_traceability_matrix : bool
0 commit comments