-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.fs
More file actions
66 lines (57 loc) · 1.73 KB
/
Copy pathProgram.fs
File metadata and controls
66 lines (57 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
open System
open Utils
let handleConversion (file: string) (mode: Mode) (spacesPerTab: int): string =
match mode with
| ToTabs -> convertSpacesToTabs file spacesPerTab
| ToSpaces -> convertTabsToSpaces file spacesPerTab
| StripOnly -> stripTrailingWhitespace file
| _ ->
printfn "You shouldn't be here"
exit 2
let runProgram (args: string[]): int =
let flags, nonFlags = splitArgs args
let filename = extractFilename nonFlags
let spacesPerTab = extractSpacesPerTab flags
try
let text = readFile filename
let mode = detectMode flags
match mode with
| NeedsInferring ->
let mode = inferMode text
let result = handleConversion text mode spacesPerTab
writeFile filename result
| _ ->
let result = handleConversion text mode spacesPerTab
writeFile filename result
printfn "File written!"
0
with
| _ ->
printfn "File not found"
1
let printHelp () =
printfn """
Usage: tabspace <file> [flags]
Flags:
-tabs : Convert to tabs.
-spaces : Convert to spaces.
-strip : Strip trailing whitespace only.
-N : Spaces per tab (e.g., -2, -4; Default: 4).
Examples:
# Auto-detect and convert
tabspace main.c
# Convert tabs to 2-space indentation
tabspace main.c -space -2
# Convert spaces to tabs (4 spaces = 1 tab)
tabspace main.c -tab -4
# Strip trailing whitespace only
tabspace main.c -strip
"""
[<EntryPoint>]
let main args =
match Array.length args with
| 0 ->
printHelp ()
1
| _ ->
runProgram args