Skip to content

Commit

Permalink
feat: continue work on rewritting the script in Nim
Browse files Browse the repository at this point in the history
FossilOrigin-Name: 8ef9aa34cdf6dd9f39070eb8225021fe14e511b1bbe1715834b0fcfc43b2aff0
  • Loading branch information
thindil committed Dec 20, 2024
1 parent de35aa9 commit 4e81d03
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/beastcleaner.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,85 @@
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

## The main module of the program.

import std/[os, parseopt]
import contracts

proc showCommandLineHelp() {.sideEffect, raises: [], tags: [WriteIOEffect],
contractual.} =
## Show the program arguments help
##
## Return QuitSuccess when the program's arguments help was shown, otherwise
## QuitFailure.
body:
try:
stdout.writeLine(x = """Available options are:
-h, --help - Show this help and quit
-v, --version - Show the program version info
Available arguments are:
clean - Clean interactively the system""")
stdout.flushFile
except IOError:
when isMainModule:
quit QuitFailure
when isMainModule:
quit QuitSuccess

proc showProgramVersion() {.sideEffect, raises: [], tags: [WriteIOEffect],
contractual.} =
## Show the program version
##
## Returns QuitSuccess when the program's arguments help was shown, otherwise
## QuitFailure.
body:
try:
stdout.writeLine(x = """
Beastcleaner version: 0.1.0
Copyright: 2024 Bartek Jasicki <[email protected]>
License: 3-Clause BSD""")
stdout.flushFile
except IOError:
when isMainModule:
quit QuitFailure
when isMainModule:
quit QuitSuccess


proc main() {.raises: [], tags: [ReadIOEffect, WriteIOEffect], contractual.} =
## The main procedure of the program
var options: OptParser = initOptParser(shortNoVal = {'h', 'v'}, longNoVal = @[
"help", "version"])
type Setting = string
const
awkScript: Setting = "/tmp/beastcleaner.awk"
pkgList: Setting = "/tmp/list1.txt"
filesList: Setting = "/tmp/list2.txt"
filesDiff: Setting = "/tmp/beastdiff.txt"

# Check the program's arguments and options
while true:
options.next
case options.kind
of cmdEnd:
break
of cmdShortOption, cmdLongOption:
case options.key
of "h", "help":
showCommandLineHelp()
of "v", "version":
showProgramVersion()
else:
quit "Unknown option '" & options.key & "'. To see all available options, run the program with --help."
of cmdArgument:
case options.key:
of "clean":
if not isAdmin():
quit "To clean the system, please run the program as root, via su, sudo, dosu, etc."
else:
quit "Unknown argument '" & options.key & "'. To see all available arguments, run the program with --help."

main()

0 comments on commit 4e81d03

Please sign in to comment.