11package com .mycmd .commands ;
22
3+ import com .mycmd .Command ;
34import com .mycmd .ShellContext ;
5+
6+ import java .io .IOException ;
47import java .util .Map ;
58
69public class AliasCommand implements Command {
710 @ Override
8- public void execute (String [] args , ShellContext context ) {
9- if ( args . length == 0 ) {
10- // List all aliases
11+ public void execute (String [] args , ShellContext context ) throws IOException {
12+ // no args: list aliases
13+ if ( args == null || args . length == 0 ) {
1114 Map <String , String > aliases = context .getAliases ();
1215 if (aliases .isEmpty ()) {
1316 System .out .println ("No aliases defined." );
1417 } else {
15- System .out .println ("Current aliases:" );
16- for (Map .Entry <String , String > entry : aliases .entrySet ()) {
17- System .out .println (" " + entry .getKey () + "=\" " + entry .getValue () + "\" " );
18- }
19- }
20- } else {
21- // Create alias: alias name="command" or alias name=command
22- String input = String .join (" " , args );
23-
24- if (!input .contains ("=" )) {
25- System .out .println ("Usage: alias name=\" command\" " );
26- System .out .println ("Example: alias ll=\" dir /w\" " );
27- return ;
18+ aliases .forEach ((k , v ) -> System .out .println (k + "=" + v ));
2819 }
20+ return ;
21+ }
2922
30- String [] parts = input .split ("=" , 2 );
23+ // single arg of form name=command
24+ if (args .length == 1 && args [0 ].contains ("=" )) {
25+ String [] parts = args [0 ].split ("=" , 2 );
3126 String name = parts [0 ].trim ();
32- String command = parts [1 ].trim ();
33-
34- // Remove quotes if present
35- if (command .startsWith ("\" " ) && command .endsWith ("\" " )) {
36- command = command .substring (1 , command .length () - 1 );
37- }
38-
39- // Validate alias name
40- if (name .isEmpty () || name .contains (" " )) {
41- System .out .println ("Error: Alias name cannot be empty or contain spaces." );
27+ String cmd = parts [1 ].trim ();
28+ if (name .isEmpty () || cmd .isEmpty ()) {
29+ System .out .println ("Invalid alias format. Usage: alias name=command" );
4230 return ;
4331 }
32+ context .addAlias (name , cmd );
33+ System .out .println ("Alias added: " + name + "=" + cmd );
34+ return ;
35+ }
4436
45- if (command .isEmpty ()) {
46- System .out .println ("Error: Alias command cannot be empty." );
47- return ;
37+ // multiple args: first is name, rest form command
38+ if (args .length >= 2 ) {
39+ String name = args [0 ];
40+ StringBuilder sb = new StringBuilder ();
41+ for (int i = 1 ; i < args .length ; i ++) {
42+ if (i > 1 ) sb .append (' ' );
43+ sb .append (args [i ]);
4844 }
49-
50- // Check for circular reference
51- if (command .split ("\\ s+" )[0 ].equalsIgnoreCase (name )) {
52- System .out .println ("Error: Cannot create circular alias reference." );
45+ String cmd = sb .toString ();
46+ if (name .trim ().isEmpty () || cmd .trim ().isEmpty ()) {
47+ System .out .println ("Invalid alias. Usage: alias name command... or alias name=command" );
5348 return ;
5449 }
55-
56- context . addAlias ( name , command );
57- System . out . println ( "Alias created: " + name + "= \" " + command + " \" " ) ;
50+ context . addAlias ( name , cmd );
51+ System . out . println ( "Alias added: " + name + "=" + cmd );
52+ return ;
5853 }
54+
55+ System .out .println ("Invalid usage. Usage: alias [name=command] | alias [name command...]" );
56+ }
57+
58+ @ Override
59+ public String description () {
60+ return "Create or list command aliases." ;
5961 }
6062
6163 @ Override
62- public String getHelp () {
63- return "alias [name=\" command\" ] - Create or list command aliases\n " +
64- " alias - List all aliases\n " +
65- " alias name=\" command\" - Create new alias\n " +
66- " Example: alias ll=\" dir /w\" " ;
64+ public String usage () {
65+ return "alias # list aliases\n " +
66+ "alias name=command # create alias\n " +
67+ "alias name command... # create alias" ;
6768 }
6869}
0 commit comments