Skip to content

Commit 9ce0c68

Browse files
committed
Updated documentation and added a sample Convert command.
1 parent 5b0ac9e commit 9ce0c68

File tree

4 files changed

+86
-10
lines changed

4 files changed

+86
-10
lines changed

Docs/Design_command.md

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,59 @@ public class ConvertCommand : CommandBase<PowerCommandsConfiguration>
9696
```
9797
**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.
9898

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!
101101
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.
102105
```
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)
106+
using System.Text.Json;
107+
using PainKiller.PowerCommands.Core.Commands;
108+
using YamlDotNet.Serialization.NamingConventions;
109+
using YamlDotNet.Serialization;
110+
111+
namespace PainKiller.PowerCommands.MyExampleCommands.Commands;
112+
113+
[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)
106119
{
107120
public override RunResult Run()
108121
{
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)
132+
{
133+
var deserializer = new DeserializerBuilder()
134+
.WithNamingConvention(CamelCaseNamingConvention.Instance)
135+
.Build();
136+
var yamlObject = deserializer.Deserialize<object>(yamlInput);
137+
138+
var json = JsonSerializer.Serialize(yamlObject, new JsonSerializerOptions { WriteIndented = true });
139+
return json;
112140
}
113141
}
114142
```
143+
![Alt text](images/convert_sample.png?raw=true "Describe convert command")
144+
145+
### 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:
147+
148+
```convert PowerCommandsConfiguration.yaml --format json | file --target "PowerCommandsConfiguration.json"```
149+
150+
This will write a file named **PowerCommandsConfiguration.json** in the current working directory.
151+
115152

116153
## Must I use the PowerCommandDesign attribute on every command I create?
117154
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
121158
Next step is to understand the [Power Commands Design attribute](PowerCommandDesignAttribute.md)
122159

123160

124-
125161
Read more about:
126162

163+
[Chain command execution)](ChainCommands.md)
164+
165+
[Read and write files with FileCommand](ReadWriteFileHandler.md)
166+
127167
[Input](Input.md)
128168

129169
[Options](Options.md)

Docs/ReadWriteFileHandler.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Read more about:
108108

109109
[Configuration (see example on how you can add your bookmarks)](Configuration.md)
110110

111-
[Chain command execution)](ChainCommands.md.md)
111+
[Chain command execution)](ChainCommands.md)
112112

113113
[Input](Input.md)
114114

Docs/images/convert_sample.png

21.1 KB
Loading
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Text.Json;
2+
using PainKiller.PowerCommands.Core.Commands;
3+
using YamlDotNet.Serialization.NamingConventions;
4+
using YamlDotNet.Serialization;
5+
6+
namespace PainKiller.PowerCommands.MyExampleCommands.Commands;
7+
8+
[PowerCommandDesign( description: "Converting yaml format to json or xml format",
9+
arguments: "<filename>",
10+
options: "format",
11+
suggestions: "xml|json",
12+
example: "//Convert to json format|convert \"c:\\temp\\test.yaml\" json|//Convert to xml format|convert \"c:\\temp\\test.yaml\" xml")]
13+
public class ConvertCommand(string identifier, PowerCommandsConfiguration configuration) : CdCommand(identifier, configuration)
14+
{
15+
public override RunResult Run()
16+
{
17+
var yamlInput = File.ReadAllText(Input.SingleArgument);
18+
var format = GetOptionValue("format");
19+
if (format == "json")
20+
{
21+
var jsonOutput = ConvertYamlToJson(yamlInput);
22+
WriteLine(jsonOutput);
23+
}
24+
return Ok();
25+
}
26+
public static string ConvertYamlToJson(string yamlInput)
27+
{
28+
var deserializer = new DeserializerBuilder()
29+
.WithNamingConvention(CamelCaseNamingConvention.Instance)
30+
.Build();
31+
var yamlObject = deserializer.Deserialize<object>(yamlInput);
32+
33+
var json = JsonSerializer.Serialize(yamlObject, new JsonSerializerOptions { WriteIndented = true });
34+
return json;
35+
}
36+
}

0 commit comments

Comments
 (0)