File tree Expand file tree Collapse file tree 21 files changed +285
-0
lines changed
src/main/java/com/mycmd/commands Expand file tree Collapse file tree 21 files changed +285
-0
lines changed Original file line number Diff line number Diff line change 44import com .mycmd .ShellContext ;
55import java .io .File ;
66
7+ /**
8+ * Changes the current working directory or displays the current directory path.
9+ *
10+ * This command allows navigation through the file system hierarchy. When called
11+ * without arguments, it prints the absolute path of the current directory. When
12+ * provided with a path argument, it attempts to change to that directory.
13+ *
14+ * Usage:
15+ * - cd : Display current directory
16+ * - cd path : Change to specified path (absolute or relative)
17+ * - cd .. : Navigate to parent directory
18+ *
19+ * The command handles both absolute and relative paths. Relative paths are
20+ * resolved against the current working directory stored in ShellContext.
21+ */
722public class CdCommand implements Command {
823 @ Override
924 public void execute (String [] args , ShellContext context ) {
Original file line number Diff line number Diff line change 33import com .mycmd .Command ;
44import com .mycmd .ShellContext ;
55
6+ /**
7+ * Clears the console screen by printing multiple blank lines.
8+ *
9+ * This command simulates clearing the terminal screen by outputting 50 blank
10+ * lines, pushing previous content out of view. This is a simple cross-platform
11+ * approach that doesn't rely on terminal-specific control sequences.
12+ *
13+ * Usage: cls
14+ *
15+ * Note: This command does not accept any arguments.
16+ */
617public class ClsCommand implements Command {
718 @ Override
819 public void execute (String [] args , ShellContext context ) {
Original file line number Diff line number Diff line change 33import com .mycmd .Command ;
44import com .mycmd .ShellContext ;
55
6+ /**
7+ * Changes the console text and background colors using ANSI escape codes.
8+ *
9+ * This command accepts a two-character hexadecimal code where the first digit
10+ * represents the background color and the second digit represents the text color.
11+ * Valid color codes are 0-9 and A-F (hexadecimal). When called without arguments,
12+ * it resets colors to terminal defaults.
13+ *
14+ * Usage:
15+ * - color : Reset to default colors
16+ * - color XY : Set background to X and text to Y (hex digits 0-F)
17+ *
18+ * Example: color 0A sets black background with bright green text.
19+ *
20+ * Note: Background and text colors cannot be the same value.
21+ */
622public class ColorCommand implements Command {
723 @ Override
824 public void execute (String [] args , ShellContext context ) {
Original file line number Diff line number Diff line change 44import com .mycmd .ShellContext ;
55import java .io .*;
66
7+ /**
8+ * Copies a file from source to destination location.
9+ *
10+ * This command reads the contents of a source file and writes it to a
11+ * destination file using buffered I/O streams for efficient copying. Both
12+ * source and destination paths are resolved relative to the current working
13+ * directory unless absolute paths are provided.
14+ *
15+ * Usage: copy source destination
16+ *
17+ * The command verifies that the source exists and is a regular file before
18+ * attempting the copy operation. If the destination file already exists, it
19+ * will be overwritten.
20+ */
721public class CopyCommand implements Command {
822 @ Override
923 public void execute (String [] args , ShellContext context ) {
Original file line number Diff line number Diff line change 44import com .mycmd .ShellContext ;
55import java .io .File ;
66
7+ /**
8+ * Deletes one or more files from the file system.
9+ *
10+ * This command accepts one or more file names as arguments and attempts to
11+ * delete each file. File paths are resolved relative to the current working
12+ * directory. The command provides feedback for each file indicating whether
13+ * the deletion was successful or if the file was not found.
14+ *
15+ * Usage: del file1 [file2 file3 ...]
16+ *
17+ * Note: This command only deletes files, not directories. Use rmdir for
18+ * directory removal.
19+ */
720public class DelCommand implements Command {
821 @ Override
922 public void execute (String [] args , ShellContext context ) {
Original file line number Diff line number Diff line change 44import com .mycmd .ShellContext ;
55import java .io .File ;
66
7+ /**
8+ * Lists files and directories in the current working directory.
9+ *
10+ * This command displays all files and subdirectories within the current
11+ * directory. Directories are prefixed with angle brackets and "DIR" label,
12+ * while files are displayed with spacing for alignment.
13+ *
14+ * Usage: dir
15+ *
16+ * Output format:
17+ * - Directories: angle-bracket-DIR-angle-bracket followed by directory name
18+ * - Files: Six spaces followed by file name
19+ *
20+ * If the directory is empty or cannot be read, an appropriate message is displayed.
21+ */
722public class DirCommand implements Command {
823 @ Override
924 public void execute (String [] args , ShellContext context ) {
Original file line number Diff line number Diff line change 33import com .mycmd .Command ;
44import com .mycmd .ShellContext ;
55
6+ /**
7+ * Displays text messages to the console output.
8+ *
9+ * This command prints all provided arguments to standard output, joining
10+ * multiple arguments with spaces. When called without arguments, it prints
11+ * a blank line.
12+ *
13+ * Usage:
14+ * - echo : Print a blank line
15+ * - echo message : Print the message to console
16+ *
17+ * Multiple words are automatically joined with spaces between them.
18+ */
619public class EchoCommand implements Command {
720 @ Override
821 public void execute (String [] args , ShellContext context ) {
Original file line number Diff line number Diff line change 33import com .mycmd .Command ;
44import com .mycmd .ShellContext ;
55
6+ /**
7+ * Terminates the MyCMD shell application.
8+ *
9+ * This command exits the shell by calling System.exit(0), which immediately
10+ * terminates the JVM process with a success status code. A goodbye message
11+ * is displayed before exiting.
12+ *
13+ * Usage: exit
14+ *
15+ * Note: This command does not accept any arguments and exits immediately
16+ * without prompting for confirmation.
17+ */
618public class ExitCommand implements Command {
719 @ Override
820 public void execute (String [] args , ShellContext context ) {
Original file line number Diff line number Diff line change 44import com .mycmd .ShellContext ;
55import java .util .Map ;
66
7+ /**
8+ * Displays a list of all available commands in the shell.
9+ *
10+ * This command provides users with an overview of commands registered in
11+ * the shell. It requires access to the command registry (Map) which is
12+ * provided during construction. Each command name is printed on a separate
13+ * line with a bullet point prefix.
14+ *
15+ * Usage: help
16+ *
17+ * The command iterates through all keys in the command registry and displays
18+ * them in the order provided by the map's key set.
19+ */
720public class HelpCommand implements Command {
821 private final Map <String , Command > commands ;
922
Original file line number Diff line number Diff line change 55import com .mycmd .Command ;
66import com .mycmd .ShellContext ;
77
8+ /**
9+ * Displays the hostname of the current computer.
10+ *
11+ * This command attempts to retrieve the system hostname using two methods:
12+ * first by checking the COMPUTERNAME environment variable (Windows-specific),
13+ * and if that fails, by using InetAddress.getLocalHost().getHostName().
14+ * If both methods fail, it displays "Unknown Host".
15+ *
16+ * Usage: hostname
17+ *
18+ * Note: This command works best on Windows systems where the COMPUTERNAME
19+ * environment variable is typically set.
20+ */
821public class HostnameCommand implements Command {
922 @ Override
1023 public void execute (String [] args , ShellContext context ) {
You can’t perform that action at this time.
0 commit comments