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
Youcanaddtothesetofmagiccommandsavailableinyournotebooks. Amagiccommandisdefinedusing [System.CommandLine](https://learn.microsoft.com/en-us/dotnet/standard/commandline/) to parse the user's input as well as provide help and completions. A `System.CommandLine.Command` is used to define a magic command. The handler you define for the command will be invoked when the user runs the magic command.
40
+
Youcancreateyouownmagiccommandsandaddthemtotheavailablecommandsinyournotebookseitherbywritinganextensionorbydefiningthemdirectlyinthenotebookcode. MagiccommandsaredefinedusingtheAPIsunderthe `Microsoft.DotNet.Interactive.Directives` namespacetoparsetheuser's input and define the associated actions performed by the magic command, as well as provide hover help and completions.
41
41
42
-
Here's an example from the `ClockExtension` sample, showing how to define the `#!clock` magic command:
42
+
We'll use the example found in the [`ClockExtension` sample](https://github.com/dotnet/interactive/blob/main/samples/extensions/ClockExtension) to go over the high-level usage of this API. This sample creates a magic command called `#!clock` that can be used to display an SVG rendering of a clock showing the specified time.
Let's start by looking at the code used to define the magic command itself. This code creates a magic command (`#!clock`) with three parameters (`--hour`, `--minute`, and `--second`), which can be called as in the screen shot above. We'llgothroughthecodestepbysteptoexplainhowtheAPIisused.
varclockCommand=newCommand("#!clock", "Displays a clock showing the current or specified time.")
59
-
{
60
-
hourOption,
61
-
minuteOption,
62
-
secondOption
63
-
};
64
-
65
-
//...
66
-
67
-
kernel.AddDirective(clockCommand);
68
-
69
-
// ...
70
-
}
71
-
}
55
+
Description="Displays a clock showing the current or specified time.",
56
+
Parameters=
57
+
[
58
+
hourParameter,
59
+
minuteParameter,
60
+
secondParameter
61
+
]
62
+
};
72
63
```
73
64
74
-
Oncethe `Command` hasbeenaddedusing `Kernel.AddDirective`, it's available in the kernel and ready to be used.
65
+
A `KernelActionDirective` is used to define a magic command and give it a name (`#!clock`), which is the text used in code to invoke it. The `KernelDirectiveParameter` class is used to define the parameters that the magic command accepts.
*`kernel.AddDirective` adds the the directive (`clockDirective`) to the kernel.
90
+
* The generic parameter (`DisplayClock`) defines the type of the associated `KernelCommand` that will be instantiated and sent to the kernel when the magic command is invoked.
91
+
* The delegate defines the code that will run to handle the `DisplayClock` command.
Note that `KernelDirectiveCommand` inherits `KernelCommand`, so you can send the `DisplayClock` command directly using the `Kernel.SendAsync` method, just like any other `KernelCommand`.
95
105
106
+
### Customize formatting
96
107
97
108
The extension also changes the default formatting for the `System.DateTime` type. This feature is the basis for creating custom visualizations for any .NET type. Before installing the extension, the default output just used the `DateTime.ToString` method:
0 commit comments