added flags to the parser and ls
This commit is contained in:
parent
45ba77c3fb
commit
f0cbfb1db5
@ -37,7 +37,7 @@ func Run_command(in input_parser.Input, env *environment.Env) {
|
||||
case "echo":
|
||||
echo.Echo(in, env)
|
||||
case "ls":
|
||||
ls.Ls(in, env, "flags")
|
||||
ls.Ls(in, env)
|
||||
case "cd":
|
||||
cd.Cd(in, env)
|
||||
case "man":
|
||||
|
||||
@ -12,8 +12,8 @@ func Help(in input_parser.Input, _ *environment.Env) {
|
||||
The options are:
|
||||
pwd: Output current working directory
|
||||
echo: Echoes the input argument
|
||||
ls: Lists Sources in the current working directory
|
||||
cd: Change to directory given in the argument
|
||||
ls: Lists Sources in the current working directory
|
||||
cd: Change to directory given in the argument
|
||||
man: Manual for the terminal options
|
||||
cat: Outputs the contents of the given file
|
||||
head: Outputs first lines of a given file. 10 if non provided. Argument <file> <number of lines>
|
||||
|
||||
@ -5,14 +5,14 @@ import (
|
||||
"bbash/input_parser"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"slices"
|
||||
)
|
||||
|
||||
var flagsArray []string = []string{"a ", "help "}
|
||||
var flagsDictionary map[string]string = map[string]string{"a": "-a shows hidden directories", "help": "-help shows this message"}
|
||||
|
||||
func Ls(in input_parser.Input, env *environment.Env, flags string) {
|
||||
if flags == "help" || flags == "-help" {
|
||||
func Ls(in input_parser.Input, env *environment.Env) {
|
||||
if slices.Contains(in.Flags, "help") {
|
||||
fmt.Printf("Lists Sources in the current working directory")
|
||||
fmt.Printf("Supported flags are:")
|
||||
for i := 0; i < len(flagsArray); i++ {
|
||||
@ -24,7 +24,7 @@ func Ls(in input_parser.Input, env *environment.Env, flags string) {
|
||||
fmt.Print(fmt.Sprintf("Error opening directory %s", env.Path))
|
||||
}
|
||||
for _, f := range fs {
|
||||
if (f.Name()[0] == '.') && (strings.Contains(flags, "a")) {
|
||||
if (f.Name()[0] == '.') && (slices.Contains(in.Flags, "a")) {
|
||||
continue
|
||||
} // allows for hidden directories
|
||||
fmt.Print(f.Name())
|
||||
|
||||
@ -12,7 +12,8 @@ func Rm(in input_parser.Input, env *environment.Env) {
|
||||
file_path := filepath.Join(env.Path, in.Args)
|
||||
stat, err := os.Stat(file_path)
|
||||
if os.IsNotExist(err) {
|
||||
fmt.Print(fmt.Sprintf("File does not exist"))
|
||||
fmt.Println(fmt.Sprintf("File does not exist"))
|
||||
fmt.Print(fmt.Sprintf("Try 'bash rm -rf /' instead"))
|
||||
return
|
||||
}
|
||||
if stat.IsDir() {
|
||||
@ -25,7 +26,3 @@ func Rm(in input_parser.Input, env *environment.Env) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -6,31 +6,53 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Input struct {
|
||||
// The instruction a.k.a first argument
|
||||
Instruction string
|
||||
Instruction string
|
||||
|
||||
// The args, currently just the string after the instruction
|
||||
Args string
|
||||
Args []string
|
||||
Args_raw string
|
||||
Flags []string
|
||||
}
|
||||
func Parse() Input {
|
||||
|
||||
func Parse() Input {
|
||||
input := input_str()
|
||||
return parse(input)
|
||||
}
|
||||
func parse(input string) Input {
|
||||
split := strings.Split(string(input), " ")
|
||||
instruction := strings.TrimSpace(split[0])
|
||||
var flags []string
|
||||
var args []string
|
||||
for i := 0; i < len(split); i++ {
|
||||
if split[i][0:2] == "--" {
|
||||
var result = strings.TrimSpace(split[i])
|
||||
flags = append(flags, string(result[2:]))
|
||||
continue
|
||||
} else if split[i][0] == '-' {
|
||||
var result = strings.TrimSpace(split[i])
|
||||
for _, c := range result[1:] {
|
||||
flags = append(flags, string(c))
|
||||
}
|
||||
continue
|
||||
} else if i != 0 {
|
||||
args = append(args, strings.TrimSpace(split[i]))
|
||||
}
|
||||
}
|
||||
|
||||
return Input{
|
||||
Instruction: instruction,
|
||||
Args: args,
|
||||
Flags: flags,
|
||||
}
|
||||
}
|
||||
func input_str() string {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
input, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
split := strings.SplitN(string(input), " ", 2)
|
||||
instruction := strings.TrimSpace(split[0])
|
||||
var arg string
|
||||
if len(split) == 2 {
|
||||
arg = strings.TrimSpace(split[1])
|
||||
} else {
|
||||
arg = ""
|
||||
}
|
||||
|
||||
return Input {
|
||||
Instruction: instruction,
|
||||
Args: arg,
|
||||
}
|
||||
return input
|
||||
}
|
||||
|
||||
|
||||
@ -1,16 +1,13 @@
|
||||
package input_parser
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
Parse("")
|
||||
Parse("hej")
|
||||
got := Parse("echo \" jej asdsa dsa \"")
|
||||
fmt.Println(got.Instruction)
|
||||
fmt.Println(got.Args)
|
||||
|
||||
|
||||
test := parse("ls -abcd hej -ee tbtry --help")
|
||||
fmt.Println(test.Instruction)
|
||||
fmt.Println(test.Args)
|
||||
fmt.Println(test.Flags)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user