Skip to content

Commit 662c46c

Browse files
committed
HeadCommand with common flags
- Displays the first part of files. - Implemented flags -n for specifying the number of lines and -c for specifying the number of bytes. - Handles multiple file inputs with appropriate headers for each file. - Defaults to 10 lines and error handling for mandatory arguments with flags.
1 parent 1df063b commit 662c46c

File tree

2 files changed

+103
-3
lines changed

2 files changed

+103
-3
lines changed
Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,103 @@
11
package com.unixtools.command.filecontent;
22

3-
public class HeadCommand {
4-
3+
import com.unixtools.core.Command;
4+
5+
import java.io.*;
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
public class HeadCommand implements Command {
12+
private static final String VALID_FLAGS = "nc";
13+
private int numberOfLines = 10;
14+
private int numberOfBytes = -1;
15+
private boolean isBytesFlagUsed = false;
16+
17+
@Override
18+
public void execute(String[] args) {
19+
List<String> flags = new ArrayList<>();
20+
List<String> filePaths = new ArrayList<>();
21+
parseArguments(args, flags, filePaths);
22+
23+
if (!areFlagsValid(flags)) {
24+
System.out.println("Invalid flags. Valid flags are: " + VALID_FLAGS);
25+
return;
26+
}
27+
28+
if (filePaths.isEmpty()) {
29+
System.out.println("head: No file specified.");
30+
return;
31+
}
32+
33+
for (String filePath : filePaths) {
34+
if (filePaths.size() > 1) {
35+
System.out.println("==> " + filePath + " <==");
36+
}
37+
displayFileContents(normalizePath(filePath));
38+
}
39+
}
40+
41+
private void parseArguments(String[] args, List<String> flags, List<String> paths) {
42+
for (int i = 0; i < args.length; i++) {
43+
if (args[i].startsWith("-")) {
44+
switch (args[i]) {
45+
case "-n":
46+
numberOfLines = Integer.parseInt(args[++i]);
47+
break;
48+
case "-c":
49+
isBytesFlagUsed = true;
50+
numberOfBytes = Integer.parseInt(args[++i]);
51+
break;
52+
default:
53+
flags.add(args[i]);
54+
break;
55+
}
56+
} else {
57+
paths.add(args[i]);
58+
}
59+
}
60+
}
61+
62+
private boolean areFlagsValid(List<String> flags) {
63+
return flags.stream().allMatch(flag -> VALID_FLAGS.contains(flag.replace("-", "")));
64+
}
65+
66+
private void displayFileContents(String filePath) {
67+
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
68+
if (isBytesFlagUsed) {
69+
readBytes(reader, numberOfBytes);
70+
} else {
71+
readLines(reader, numberOfLines);
72+
}
73+
} catch (IOException e) {
74+
System.out.println("Error reading file: " + e.getMessage());
75+
}
76+
}
77+
78+
private void readLines(BufferedReader reader, int numLines) throws IOException {
79+
for (int i = 0; i < numLines; i++) {
80+
String line = reader.readLine();
81+
if (line == null) {
82+
break;
83+
}
84+
System.out.println(line);
85+
}
86+
}
87+
88+
private void readBytes(BufferedReader reader, int numBytes) throws IOException {
89+
int remainingBytes = numBytes;
90+
int ch;
91+
while (remainingBytes > 0 && (ch = reader.read()) != -1) {
92+
System.out.print((char) ch);
93+
remainingBytes--;
94+
}
95+
System.out.println();
96+
}
97+
98+
private String normalizePath(String path) {
99+
String normalizedPath = path.replace("\\", "/");
100+
Path pathObj = Paths.get(normalizedPath).normalize();
101+
return pathObj.toString();
102+
}
5103
}

src/com/unixtools/core/CommandFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.unixtools.core;
22

33
import com.unixtools.command.filemanagement.*;
4-
import com.unixtools.command.filecontent.*;
54
import com.unixtools.command.networking.*;
5+
import com.unixtools.command.filecontent.*;
66

77
public class CommandFactory {
88
public static Command getCommand(String commandName) {
@@ -52,6 +52,8 @@ public static Command getCommand(String commandName) {
5252
return new EchoCommand();
5353
case "cat":
5454
return new CatCommand();
55+
case "head":
56+
return new HeadCommand();
5557
default:
5658
return null;
5759
}

0 commit comments

Comments
 (0)