#anyCLI_v02.py def anyCLI_help(vsn): #by D@CC #on 2024CMar09 #from n.py #Purpose: to be used by any python CLI program #Called by: main print("#:::: Help for python anyCLI.py "+vsn+" [IX grep program]") print("#:::: Syntax: ./anyCLI.py [args]/target/ "') print("#:::: (Cudos to:O'Reilly for textpack.py)") print("#:::: Source 01: http://index-of.es/Programming/Python/O'Reilly%20-%20Programming%20Python,%203rd%20Ed%20Aug2006.pdf") print("#:::: Source 02: grep.py in Article 215") print("#:::: 'python anyCLI.py -f/target/ -1 : # always show markers #print("@84") if(isMequalsM): # is it a marker line #print("@86 marker found in line ",lNo) markCnt+=1 filename="" # in case llen is bad if True : llen=len(line) filename=line[mlen:llen] # get filename #if end #print("@93 #",lNo, "m "+filename) print(lNo," m "+filename) else: pass #print("@97 m NOT = m") #if end #if end #if argList.find("o")>-1: functionList on line # also show oreilly markers if(line[:olen]== omarker): # is it a o-marker line markCnt+=1 llen=len(line) filename=line[olen:llen-1] # get filename without LF print() print("@106 #",lNo, "o "+filename+" oooooooo Oreilly marker oooooooo") print() #if end #process each line as follows if lNo<10000 : if lNo==1: pass #print("@114 processing line #",lNo) else: pass #print("@117 processing line #",lNo) #if end if argStr.find("f")>-1 : # if option includes "f" if line.find(targStr)>-1: cntTarget+=1 # count times found #print("@122 targetStr:"+targetStr+":") print(lNo,' "'+targStr+'": ',line) #print("EOL") else: pass #print("@127:",line.find(targetStr)) #if end #if end else: pass #print("@132:") #if end #for end of MAIN FOR loop #print("@135 returning from process routine") return lNo,cntTarget,markCnt #def end processLineForGrep() def funcCLI(testStr, s, v=1) : #by D@CC #on 2024CMar09 #Purpose: test pass; vv=1 #returnValue = functionList_v01(testStr) returnValue = "response from funcA" if v=="?" : return returnValue,vv else : return returnValue #if end #def end funcCLI( #! /usr/bin/python3 #anyCLI.py # !/usr/local/bin/python deprecated for Raspberry Pi # # by D@CC on 2024CMar08 #################################################################### # first line (shebang) must be #! /usr/bin/python3 # # place anyCLI.py and the package into the sandBox # # version: v02 # # Usage: >$ cd Desktop/ # # >sudo chmod +x anyCLI.py # # >$ python3 ../anyCLI.py < ../adcRead_pkg.py # # or: >$ cd Desktop # # >$ ./anyCLI.p -options < adcRead_pkg.py # # NB In terminal, the above should list the names of the # # files that exist in the package or file named in stdin. # # NB recently added is the listing of used-functions i.e. "calls" # # Due to the Unix trick line 1 "#!/usr... # # and the last line if __name__ ... mainroutine() # # the command line below works (afer chmod ...): # # >$ ./anyCLI.p -all ' # the IX Marker is different omarker = ':'*10 + 'textpak=>' # the original O'Reilly Marker mlen = len(marker) # file names appear after markers olen = len(omarker) #if end aType=type(sys.argv) #print("@218 aType:",aType,":") #print("@225") if type(sys.argv)==list : argv0="" argv1="" argv2="" argv3="" aLen=len(sys.argv) #print("@232 aLen",aLen) if aLen>(0) : argv0=str(sys.argv[0]) #print("argv0:",argv0) if aLen>1 : argv1=str(sys.argv[1]) #print("argv1:",argv1) #print("@240") if aLen>2 : argv2=str(sys.argv[2]) #print("argv2:",argv2) if aLen>3 : argv3=str(sys.argv[3]) #print("argv3:",argv3) #if end if argv3=="" : isAnyStdIn=False print("@252 Note Only 1 file will be read") else: isAnyStdIn=True # read 1 file, not stdInput print("@255 Note stdInput of multiple files") #if end #if end print() #print("@260") progName=argv0 argList =argv1 targStr="" cmdList="" fileName0="" fileName1="" fileExt="" #print("@268 argList:"+argList+":") if argList[0:1]=="-": #print("@270") cmdList=argList targP0=argList.find("/") targStr="" #print("@274 targP0:",targP0) if targP0>-1: targPEnd=argList.find("/",targP0+1) #bad statement #print("@277 targPEnd:",targPEnd) #print("@278 argList:"+argList+":") t=targP0+1 targStr=argList[t:targPEnd] #print("@281 targStr:"+targStr+":") argList=argList[0:targP0] else: print("@284 WARNING: no first target '/'") targStr="" #print("@286 Leave argList unchanged") fileName0=argv2 #print("@288 targStr:"+targStr+":") else: argList="-a" targetStr="ToDo" cmdList="-a/ToDo/" #cmdList if null options fileName0=argv1 #if end if fileName0=="" : fileName0="/home/pi/Desktop/IX_assets/ix/eDict.txt" #Default file #if end #print("@288 fileName0:"+fileName0+":") if len(fileName0)>0 : extPos=fileName0.find(".") #print("@301 extPos:",extPos) if extPos>-1 : fileExt=fileName0[extPos+1:len(fileName0)] #print("@304 fileExt:"+fileExt+":") #if end isVerbose=False if argList.find("v")>-1 : isVerbose =True #if end print("@309 isAnyStdIn:",isAnyStdIn) argStr=argList if isVerbose : print("test using <$ python anyCLI.py") print("test using <$ python anyCLI.py *.py") print("test using <$ python anyCLI.py -a *.py") print("test using <$ python anyCLI.py -m *.py") print("test using <$ python anyCLI.py -m -1: #print("argTarg2Slash:",argTarg2Slash) argTarg=argStr[argTarg1+1:argTarg2Slash] else: argTarg="" #if end #if False end #print('@395 targStr:'+targStr+":") #prints SHELL #targetStr=argTarg #cmdList=cmdList0 #print("@400 cmdList:"+cmdList+":") if False : #print("argTarg1:",argTarg1) argLen=len(argStr) #print("argLen:",argLen) argTargT=argStr[argTarg1+1:argTarg2Slash] #print('argTargT:',argTargT) #prints [-a/SHELL/] argTargEnd=argStr[argLen:1] print() #print("@409 argTargEnd:",argTargEnd) #targetStr="def" #user should define this on the CLI print("targetStr:"+targetStr+":") if argStr=="": cmdList0="-f/ToDo/" print("@414 cmdList:"+cmdList+":") targetStr="ToDo" #cmdList0="-f/def/" #targetStr="def" fileExt="" else: #print("@420 argStr:"+argStr) argTarg1=argStr.find("/") #print("@422 argTarg1:",argTarg1) cmdList=argStr[1:argTarg1-1] #print("@424 cmdList:"+cmdList+":") #print("@425 targetStr:"+targetStr+":") fileExt="" #if end #if end #print("@429 beginning after setup, "+progName+".py") #print() if True : #argTarg1=argList.find("/") #needed this to set target = "ToDo" isBadArg=False if fileExt==".py": isBadArg=True #flag a Bad Argument if fileExt.find("pkg")>-1 : isBadArg=True #flag a Bad Argument #if end #if end if isBadArg==True: pass print('@443 ERROR Be sure to precede the Pkg name with a "<".') return #from mainroutine else: #print("@446") #print("@437 cmdList0:"+cmdList0) if False: pass else: pass #if end #if end #print("@454 DEBUG bad indentation") if True : #print("@467 what was lost prior to this") while True: #for cmdList in sys.argv[1:]: if False : #FALSE FALSE if argList=="[]": #if argList is empty cmdList=cmdList0 #cmdList0="-f/ToDo/" else: #print("@474 argList:"+argList) ##argStr argTarg1=argList.find("/") #print("@476 argTarg1:",argTarg1) cmdList=argStr[2:argTarg1-0] #if end #if False end print("@480 argStr:"+argStr+":") #print("@481 cmdList:"+cmdList+":") #print("@482 targStr:"+targStr+":") if cmdList.find("h")>-1 : uS="_" anyCLI_help(vsn) # prints help text v="unknown" print("#:::: mainroutine:anyCLI: "+vsn) print("#:::: funcCLI vsn: v",funcCLI("",0,"?") ) return #from mainroutine() #if end if cmdList.find("all")>-1 : cmdList="-mf" #all means list all (except h_) #endif if cmdList.find("a")>-1 : cmdList="-fm" #a means list all (except hu) #endif if cmdList.find("<")>-1 : print("#:::: ERROR precede the < with a space") return #from anyCLI.py #if end #print("#::: Enhanced anyCLI.py "+vsn+" [IX pkg names list prg -h for help]:") cntTarget=0 #print("@508 setting cntTarget=0") if True: mlen=len(marker)-1 markerUse=marker[0:mlen] markCnt=0 #print("@513 open & process each line from sys.stdin.readlines()") #process each line from stdin #print("@515 cmdList:"+cmdList+":") #print("@516 targetStr:"+targetStr+":") #file=sys.stdin.readlines( ) #file="Doh" #print("@519 file:",file) isAnyStdin=True while isAnyStdin : cntTarget=0 #print("@523 v:big argList:",argList) markCnt=0 lNo=0 line="" print("@527 cmdList:"+cmdList+":") if isAnyStdIn : #print("@531") #fileIn=sys.stdin.readlines() fileToRead="stdin" argListStripped=argList.strip('[') ##argStr argListStripped=argListStripped.strip(']') #print("@534 argListStripped:",argListStripped) #needs single quotes removed argListStripped=argListStripped.replace("'","") # needs spaces removed argListStripped=argListStripped.replace(" ","") #print("@537 argListStripped:",argListStripped) #needs single quotes removed fileArgList=argListStripped.split(",") #create list if isVerbose : pass print("@541 fileArgList:") print("@542:",fileArgList) #fileArgList***************** aType=type(fileArgList) #print("@544 aType:",aType) else: print("@546 read line(s) from fileName0") fileIn=open(fileName0,"r") fileToRead=fileName0 #if end if True : pass #print("@552 mlen:",mlen) #print("@553 mkr:"+marker) #if end #print("@555 fileToRead:",fileToRead) print() print("if prompted for input (or program seems to hang), Hit 'd' then ^D^D^D") if isAnyStdIn : aType=type(argList) ##argStr #print("@560 fileArgList:") #print("@561:",fileArgList) for nextFileNeedsStrip in fileArgList : #print("@563 nf:"+nextFileNeedsStrip) nextFile=nextFileNeedsStrip #nextFile=nextFile.strip(" ") #nextFile=nextFile.strip("'") #print("@567 nextFile: ",nextFile) if nextFile!=progName and nextFile!=cmdList : #print("@569 process:"+nextFile+":") isOpenedOK=True try: nextF=open(nextFile,"r") except: #print("@574 couldn't open:",nextFile) isOpenedOK=False break # out of first for at @564 finally: pass #print("@579 process this file (that was opened") #try end #print("@581 isOpenedOK:",isOpenedOK) if isOpenedOK : #print("@583 begin for lineLF of file:",nextFile) for lineLF in nextF.readlines() : #lNo was initialized in @121 # MAIN FOR all input lines #print("@588 calling processLineForGrep******************************") lNo, cntTarget,markCnt = processLineForGrep(lineLF,lNo,mlen,marker,argStr,argList,omarker,olen, \ targStr,cntTarget,markCnt) #print("@590 after each line of processLineForGrep") #for lineLF end nextF.close() #input("@593 proceed with next file?") #if end #else: # #print("@596 skipping file:"+nextFile) # pass #if nextFile end #print("@599 after for nextFile") #input("@600 proceed with next file?") #for nextFile end #if end print("@603 after processing stdIn rec count: lNo:",lNo) print("@610") if lNo==0 : print("@612 ERROR sys.stdinput was empty, needs '