-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: continue work on rewritting the script in Nim
FossilOrigin-Name: 8ef9aa34cdf6dd9f39070eb8225021fe14e511b1bbe1715834b0fcfc43b2aff0
- Loading branch information
Showing
1 changed file
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() |