summaryrefslogtreecommitdiff
path: root/src/cmd/help.zig
blob: 0a19e989e7f9e444f42b15b9c92bac6ebc98eef8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
const std = @import("std");
const types = @import("lib/types.zig");
const CommandContext = types.CommandContext;
const CommandStatus = types.CommandStatus;
const OutputCapture = types.OutputCapture;

pub const Help = struct {
    command: ?[]const u8 = null,

    pub fn eval(self: Help, ctx: CommandContext) !CommandStatus {
        if (self.command) |cmd| {
            return showCommandHelp(cmd, ctx);
        } else {
            return showGeneralHelp(ctx);
        }
    }
};

fn showGeneralHelp(ctx: CommandContext) !CommandStatus {
    const help_text =
        \\DOSE - DOS-style Command Shell
        \\
        \\Available commands:
        \\
        \\File Operations:
        \\  COPY     - Copy files
        \\  DIR      - Display directory contents
        \\  TYPE     - Display file contents
        \\  CD       - Change directory
        \\  MD       - Create directory
        \\  RD       - Remove directory
        \\  REN      - Rename files/directories
        \\  DEL      - Delete files
        \\  MOVE     - Move files/directories
        \\
        \\System Commands:
        \\  ECHO     - Display messages or control echo state
        \\  CLS      - Clear screen
        \\  DATE     - Display current date
        \\  TIME     - Display current time
        \\  VER      - Display version information
        \\  PATH     - Display or set PATH environment variable
        \\  SORT     - Sort input lines alphabetically
        \\  EXIT     - Exit the shell
        \\
        \\For help on a specific command, type: command /?
        \\Example: DIR /?
        \\
    ;

    var writer = ctx.output_writer;
    try writer.write(help_text);

    return CommandStatus{ .Code = 0 };
}

fn showCommandHelp(command: []const u8, ctx: CommandContext) !CommandStatus {
    const cmd_upper = try std.ascii.allocUpperString(ctx.allocator, command);
    defer ctx.allocator.free(cmd_upper);

    const help_text = getCommandSpecificHelp(cmd_upper);

    var writer = ctx.output_writer;
    try writer.write(help_text);

    return CommandStatus{ .Code = 0 };
}

fn getCommandSpecificHelp(command: []const u8) []const u8 {
    if (std.mem.eql(u8, command, "DIR")) {
        return 
        \\DIR - Display directory contents
        \\
        \\Syntax: DIR [path] [/flags]
        \\
        \\  path     Directory path to list (default: current directory)
        \\
        \\Flags:
        \\  /?       Display this help message
        \\  /W       Use wide list format (filenames only, multiple columns)
        \\  /B       Use bare format (filenames only, one per line)
        \\  /S       Display files in subdirectories as well
        \\
        \\Example: DIR C:\ /W
        \\
        ;
    } else if (std.mem.eql(u8, command, "COPY")) {
        return 
        \\COPY - Copy files
        \\
        \\Syntax: COPY source destination [/flags]
        \\
        \\  source      Source file or device
        \\  destination Destination file or device
        \\
        \\Flags:
        \\  /?          Display this help message
        \\  /Y          Suppress prompting to confirm overwrite
        \\
        \\Example: COPY file1.txt file2.txt
        \\
        ;
    } else if (std.mem.eql(u8, command, "TYPE")) {
        return 
        \\TYPE - Display file contents
        \\
        \\Syntax: TYPE filename [/flags]
        \\
        \\  filename    File to display
        \\
        \\Flags:
        \\  /?          Display this help message
        \\
        \\Example: TYPE readme.txt
        \\
        ;
    } else if (std.mem.eql(u8, command, "CD") or std.mem.eql(u8, command, "CHDIR")) {
        return 
        \\CD/CHDIR - Change directory
        \\
        \\Syntax: CD [path] [/flags]
        \\
        \\  path        Directory to change to
        \\
        \\Flags:
        \\  /?          Display this help message
        \\
        \\Example: CD C:\Windows
        \\
        ;
    } else if (std.mem.eql(u8, command, "ECHO")) {
        return 
        \\ECHO - Display messages or control echo state
        \\
        \\Syntax: ECHO [message] [/flags]
        \\        ECHO ON|OFF
        \\
        \\  message     Text to display
        \\
        \\Flags:
        \\  /?          Display this help message
        \\
        \\Example: ECHO Hello World
        \\
        ;
    } else if (std.mem.eql(u8, command, "CLS")) {
        return 
        \\CLS - Clear screen
        \\
        \\Syntax: CLS [/flags]
        \\
        \\Flags:
        \\  /?          Display this help message
        \\
        ;
    } else if (std.mem.eql(u8, command, "EXIT")) {
        return 
        \\EXIT - Exit the shell
        \\
        \\Syntax: EXIT [/flags]
        \\
        \\Flags:
        \\  /?          Display this help message
        \\
        ;
    } else if (std.mem.eql(u8, command, "VER")) {
        return 
        \\VER - Display version information
        \\
        \\Syntax: VER [/flags]
        \\
        \\Flags:
        \\  /?          Display this help message
        \\
        ;
    } else if (std.mem.eql(u8, command, "DATE")) {
        return 
        \\DATE - Display current date
        \\
        \\Syntax: DATE [/flags]
        \\
        \\Flags:
        \\  /?          Display this help message
        \\
        ;
    } else if (std.mem.eql(u8, command, "TIME")) {
        return 
        \\TIME - Display current time
        \\
        \\Syntax: TIME [/flags]
        \\
        \\Flags:
        \\  /?          Display this help message
        \\
        ;
    } else if (std.mem.eql(u8, command, "MD") or std.mem.eql(u8, command, "MKDIR")) {
        return 
        \\MD/MKDIR - Create directory
        \\
        \\Syntax: MD directory [/flags]
        \\        MKDIR directory [/flags]
        \\
        \\  directory   Directory path to create
        \\
        \\Flags:
        \\  /?          Display this help message
        \\
        \\Example: MD C:\TEMP
        \\Note: Creates intermediate directories if needed
        \\
        ;
    } else if (std.mem.eql(u8, command, "RD") or std.mem.eql(u8, command, "RMDIR")) {
        return 
        \\RD/RMDIR - Remove directory
        \\
        \\Syntax: RD directory [/flags]
        \\        RMDIR directory [/flags]
        \\
        \\  directory   Directory path to remove
        \\
        \\Flags:
        \\  /?          Display this help message
        \\
        \\Example: RD C:\TEMP
        \\Note: Directory must be empty
        \\
        ;
    } else if (std.mem.eql(u8, command, "REN") or std.mem.eql(u8, command, "RENAME")) {
        return 
        \\REN/RENAME - Rename files or directories
        \\
        \\Syntax: REN oldname newname [/flags]
        \\        RENAME oldname newname [/flags]
        \\
        \\  oldname     Current name of file or directory
        \\  newname     New name for file or directory
        \\
        \\Flags:
        \\  /?          Display this help message
        \\
        \\Example: REN file1.txt file2.txt
        \\
        ;
    } else if (std.mem.eql(u8, command, "MOVE")) {
        return 
        \\MOVE - Move files or directories
        \\
        \\Syntax: MOVE source destination [/flags]
        \\
        \\  source      Source file or directory
        \\  destination Destination path
        \\
        \\Flags:
        \\  /?          Display this help message
        \\
        \\Example: MOVE file1.txt C:\TEMP\file1.txt
        \\Note: MOVE command is currently a placeholder
        \\
        ;
    } else if (std.mem.eql(u8, command, "PATH")) {
        return 
        \\PATH - Display or set PATH environment variable
        \\
        \\Syntax: PATH [/flags]
        \\        PATH newpath [/flags]
        \\        PATH=newpath
        \\
        \\  newpath     New PATH value to set
        \\
        \\Flags:
        \\  /?          Display this help message
        \\
        \\Example: PATH C:\DOS;C:\UTILS
        \\Note: Without arguments, displays current PATH
        \\
        ;
    } else if (std.mem.eql(u8, command, "SORT")) {
        return 
        \\SORT - Sort input lines alphabetically
        \\
        \\Syntax: SORT [/flags]
        \\
        \\Flags:
        \\  /?          Display this help message
        \\
        \\Example: TYPE file.txt | SORT
        \\Note: Reads from standard input and sorts lines
        \\
        ;
    } else if (std.mem.eql(u8, command, "DEL") or std.mem.eql(u8, command, "ERASE")) {
        return 
        \\DEL/ERASE - Delete files
        \\
        \\Syntax: DEL filename [/flags]
        \\        ERASE filename [/flags]
        \\
        \\  filename    File to delete
        \\
        \\Flags:
        \\  /?          Display this help message
        \\
        \\Example: DEL temp.txt
        \\Note: Wildcard deletion not yet implemented
        \\
        ;
    } else {
        return 
        \\Unknown command. Type HELP for a list of commands.
        \\
        ;
    }
}