1
- // Copyright (c) .NET Foundation and contributors. All rights reserved.
1
+ // Copyright (c) .NET Foundation and contributors. All rights reserved.
2
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
4
4
using System . CommandLine . Parsing ;
@@ -13,6 +13,32 @@ public class Pipeline
13
13
public ErrorReportingSubsystem ? ErrorReporting { get ; set ; }
14
14
public CompletionSubsystem ? Completion { get ; set ; }
15
15
16
+ public ParseResult Parse ( CliConfiguration configuration , string rawInput )
17
+ => Parse ( configuration , CliParser . SplitCommandLine ( rawInput ) . ToArray ( ) ) ;
18
+
19
+ public ParseResult Parse ( CliConfiguration configuration , string [ ] args )
20
+ {
21
+ InitializeSubsystems ( configuration ) ;
22
+ var parseResult = CliParser . Parse ( configuration . RootCommand , args , configuration ) ;
23
+ return parseResult ;
24
+ }
25
+
26
+ public CliExit Execute ( CliConfiguration configuration , string rawInput , ConsoleHack ? consoleHack = null )
27
+ => Execute ( configuration , CliParser . SplitCommandLine ( rawInput ) . ToArray ( ) , rawInput , consoleHack ) ;
28
+
29
+ public CliExit Execute ( CliConfiguration configuration , string [ ] args , string rawInput , ConsoleHack ? consoleHack = null )
30
+ {
31
+ var cliExit = Execute ( Parse ( configuration , args ) , rawInput , consoleHack ) ;
32
+ return TearDownSubsystems ( cliExit ) ;
33
+ }
34
+
35
+ public CliExit Execute ( ParseResult parseResult , string rawInput , ConsoleHack ? consoleHack = null )
36
+ {
37
+ var pipelineContext = new PipelineContext ( parseResult , rawInput , this , consoleHack ?? new ConsoleHack ( ) ) ;
38
+ ExecuteSubsystems ( pipelineContext ) ;
39
+ return new CliExit ( pipelineContext ) ;
40
+ }
41
+
16
42
protected virtual void InitializeHelp ( CliConfiguration configuration )
17
43
=> Help ? . Initialize ( configuration ) ;
18
44
@@ -22,20 +48,28 @@ protected virtual void InitializeVersion(CliConfiguration configuration)
22
48
protected virtual void InitializeErrorReporting ( CliConfiguration configuration )
23
49
=> ErrorReporting ? . Initialize ( configuration ) ;
24
50
25
- protected virtual void InitializeCompletions ( CliConfiguration configuration )
51
+ protected virtual void InitializeCompletion ( CliConfiguration configuration )
26
52
=> Completion ? . Initialize ( configuration ) ;
27
53
28
- protected virtual void TearDownHelp ( PipelineContext context )
29
- => Help ? . TearDown ( context ) ;
54
+ protected virtual CliExit TearDownHelp ( CliExit cliExit )
55
+ => Help is null
56
+ ? cliExit
57
+ : Help . TearDown ( cliExit ) ;
30
58
31
- protected virtual void TearDownVersion ( PipelineContext context )
32
- => Version ? . TearDown ( context ) ;
59
+ protected virtual CliExit ? TearDownVersion ( CliExit cliExit )
60
+ => Version is null
61
+ ? cliExit
62
+ : Version . TearDown ( cliExit ) ;
33
63
34
- protected virtual void TearDownErrorReporting ( PipelineContext context )
35
- => ErrorReporting ? . TearDown ( context ) ;
64
+ protected virtual CliExit TearDownErrorReporting ( CliExit cliExit )
65
+ => ErrorReporting is null
66
+ ? cliExit
67
+ : ErrorReporting . TearDown ( cliExit ) ;
36
68
37
- protected virtual void TearDownCompletions ( PipelineContext context )
38
- => Completion ? . TearDown ( context ) ;
69
+ protected virtual CliExit TearDownCompletions ( CliExit cliExit )
70
+ => Completion is null
71
+ ? cliExit
72
+ : Completion . TearDown ( cliExit ) ;
39
73
40
74
protected virtual void ExecuteHelp ( PipelineContext context )
41
75
=> ExecuteIfNeeded ( Help , context ) ;
@@ -49,58 +83,57 @@ protected virtual void ExecuteErrorReporting(PipelineContext context)
49
83
protected virtual void ExecuteCompletions ( PipelineContext context )
50
84
=> ExecuteIfNeeded ( Completion , context ) ;
51
85
52
- protected static void ExecuteIfNeeded ( CliSubsystem ? subsystem , PipelineContext pipelineContext )
53
- {
54
- if ( subsystem is not null && ( ! pipelineContext . AlreadyHandled || subsystem . RunsEvenIfAlreadyHandled ) )
55
- {
56
- subsystem . ExecuteIfNeeded ( pipelineContext ) ;
57
- }
58
- }
59
-
60
- public virtual void InitializeExtensions ( CliConfiguration configuration )
86
+ // TODO: Consider whether this should be public. It would simplify testing, but would it do anything else
87
+ // TODO: Confirm that it is OK for ConsoleHack to be unavailable in Initialize
88
+ /// <summary>
89
+ /// Perform any setup for the subsystem. This may include adding to the CLI definition,
90
+ /// such as adding a help option. It is important that work only needed when the subsystem
91
+ ///
92
+ /// </summary>
93
+ /// <param name="configuration"></param>
94
+ /// <remarks>
95
+ /// Note to inheritors: The ordering of initializing should normally be in the reverse order than tear down
96
+ /// </remarks>
97
+ protected virtual void InitializeSubsystems ( CliConfiguration configuration )
61
98
{
62
99
InitializeHelp ( configuration ) ;
63
100
InitializeVersion ( configuration ) ;
64
101
InitializeErrorReporting ( configuration ) ;
65
- InitializeCompletions ( configuration ) ;
102
+ InitializeCompletion ( configuration ) ;
66
103
}
67
104
68
- public virtual void TearDownExtensions ( PipelineContext pipelineContext )
105
+ // TODO: Consider whether this should be public
106
+ // TODO: Would Dispose be a better alternative? This may be non-dispose like things, such as removing options?
107
+ /// <summary>
108
+ /// Perform any cleanup operations
109
+ /// </summary>
110
+ /// <param name="pipelineContext">The context of the current execution</param>
111
+ /// <remarks>
112
+ /// Note to inheritors: The ordering of tear down should normally be in the reverse order than initializing
113
+ /// </remarks>
114
+ protected virtual CliExit TearDownSubsystems ( CliExit cliExit )
69
115
{
70
- TearDownHelp ( pipelineContext ) ;
71
- TearDownVersion ( pipelineContext ) ;
72
- TearDownErrorReporting ( pipelineContext ) ;
73
- TearDownCompletions ( pipelineContext ) ;
116
+ TearDownCompletions ( cliExit ) ;
117
+ TearDownErrorReporting ( cliExit ) ;
118
+ TearDownVersion ( cliExit ) ;
119
+ TearDownHelp ( cliExit ) ;
120
+ return cliExit ;
74
121
}
75
122
76
- protected virtual void ExecuteRequestedExtensions ( PipelineContext pipelineContext )
123
+ protected virtual void ExecuteSubsystems ( PipelineContext pipelineContext )
77
124
{
78
125
ExecuteHelp ( pipelineContext ) ;
79
126
ExecuteVersion ( pipelineContext ) ;
80
127
ExecuteErrorReporting ( pipelineContext ) ;
81
128
ExecuteCompletions ( pipelineContext ) ;
82
129
}
83
130
84
- public ParseResult Parse ( CliConfiguration configuration , string rawInput )
85
- => Parse ( configuration , CliParser . SplitCommandLine ( rawInput ) . ToArray ( ) ) ;
86
-
87
- public ParseResult Parse ( CliConfiguration configuration , string [ ] args )
131
+ protected static void ExecuteIfNeeded ( CliSubsystem ? subsystem , PipelineContext pipelineContext )
88
132
{
89
- InitializeExtensions ( configuration ) ;
90
- var parseResult = CliParser . Parse ( configuration . RootCommand , args , configuration ) ;
91
- return parseResult ;
133
+ if ( subsystem is not null && ( ! pipelineContext . AlreadyHandled || subsystem . RunsEvenIfAlreadyHandled ) )
134
+ {
135
+ subsystem . ExecuteIfNeeded ( pipelineContext ) ;
136
+ }
92
137
}
93
138
94
- public CliExit Execute ( CliConfiguration configuration , string rawInput , ConsoleHack ? consoleHack = null )
95
- => Execute ( configuration , CliParser . SplitCommandLine ( rawInput ) . ToArray ( ) , rawInput , consoleHack ) ;
96
-
97
- public CliExit Execute ( CliConfiguration configuration , string [ ] args , string rawInput , ConsoleHack ? consoleHack = null )
98
- => Execute ( Parse ( configuration , args ) , rawInput , consoleHack ) ;
99
-
100
- public CliExit Execute ( ParseResult parseResult , string rawInput , ConsoleHack ? consoleHack = null )
101
- {
102
- var pipelineContext = new PipelineContext ( parseResult , rawInput , this , consoleHack ?? new ConsoleHack ( ) ) ;
103
- ExecuteRequestedExtensions ( pipelineContext ) ;
104
- return new CliExit ( pipelineContext ) ;
105
- }
106
139
}
0 commit comments