You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Docs/Design_command.md
+49-9Lines changed: 49 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -96,22 +96,59 @@ public class ConvertCommand : CommandBase<PowerCommandsConfiguration>
96
96
```
97
97
**Please Note** that the name of the arguments in the design attribute is not important in code, it is useful thou when help about the command is displayed. Suggestions is what it sounds like only suggestions to guide the user to the right input.
98
98
99
-
### Inherit CdCommand to add file/dir code completion
100
-
If you are design a command that will handle files or directories you may want to help the user with code completion, you can implement this easily just inherit the CdCommand and then your Command will support just that!
99
+
##My final design with file/dir code completion
100
+
If you are design a command that will handle files or directories you may want to help the user with code completion, you can implement this easily, just inherit the CdCommand and then your Command will support just that!
101
101
The Command below wil handle navigation trough files and directories (in current working folder) just using tab. PowerCommand has `cd` command and `dir` commands out of the box.
102
+
103
+
So here is my final design for the ConvertCommand class where I have implemented the conversion code also so this example is fully working, I have done some rearrangement of the parameters.
104
+
I want the target file to be the first thing you input so that the user can use the code completion functionality, for the format I use a option flag --format where the user values can be json or xml. This example is simplified and only support the json format, but you get the idea I hope.
102
105
```
103
-
[PowerCommandDesign(description: "Run commands that supports pipe functionality.",
104
-
example: "//First run this command and then the version command|version [PIPE] pipe")]
105
-
public class BrowseCommand(string identifier, PowerCommandsConfiguration configuration) : CdCommand(identifier, configuration)
[PowerCommandDesign( description: "Converting yaml format to json or xml format",
114
+
arguments: "<filename>",
115
+
options: "format",
116
+
suggestions: "xml|json",
117
+
example: "//Convert to json format|convert \"c:\\temp\\test.yaml\" --format json|//Convert to xml format|convert \"c:\\temp\\test.yaml\" --format xml")]
118
+
public class ConvertCommand(string identifier, PowerCommandsConfiguration configuration) : CdCommand(identifier, configuration)
106
119
{
107
120
public override RunResult Run()
108
121
{
109
-
var fileName = Input.SingleArgument;
110
-
WriteLine(fileName);
111
-
return Ok();;
122
+
var yamlInput = File.ReadAllText(Input.SingleArgument);
123
+
var format = GetOptionValue("format");
124
+
if (format == "json")
125
+
{
126
+
var jsonOutput = ConvertYamlToJson(yamlInput);
127
+
WriteLine(jsonOutput);
128
+
}
129
+
return Ok();
130
+
}
131
+
public static string ConvertYamlToJson(string yamlInput)
### User wants to write the converted output to file
146
+
You could add this to the ConvertCommand class but... you do not have to do that, PowerCommands support to call a second command on the same commandline and that second command can handle the output from the "calling" command, so if you want the output that is displayed in the console by the ConvertCommand to be written to file you could write this in the console and use the existing `file` command, you need to specify a target file to the `file` command with the option flag `--target` like this:
This will write a file named **PowerCommandsConfiguration.json** in the current working directory.
151
+
115
152
116
153
## Must I use the PowerCommandDesign attribute on every command I create?
117
154
No that is not mandatory but it is recommended, note that when you declare the [Options](Options.md), they will be available for code completion, which means that when the consumer types - and hit the tab button the user will can se what options there are that could be used, with a simple ! character you tell that the argument, quote, option or secret is required and then the Core runtime will validate that automatically for you.
@@ -121,9 +158,12 @@ Read more about CLI design here: [10 design principles for delightful CLIs](http
121
158
Next step is to understand the [Power Commands Design attribute](PowerCommandDesignAttribute.md)
122
159
123
160
124
-
125
161
Read more about:
126
162
163
+
[Chain command execution)](ChainCommands.md)
164
+
165
+
[Read and write files with FileCommand](ReadWriteFileHandler.md)
0 commit comments