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