Skip to content

Commit

Permalink
Merge pull request #16 from AdeebNqo/feature/command_line_args_cleanup
Browse files Browse the repository at this point in the history
used argparse for handling of cmdline args and updated example use
  • Loading branch information
danfolkes committed Feb 26, 2016
2 parents e21eb94 + c2bcd40 commit 71da071
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
66 changes: 50 additions & 16 deletions Magnet_To_Torrent2.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import sys
import libtorrent as lt
from time import sleep
from argparse import ArgumentParser


def magnet2torrent(magnet, output_name=None):
Expand Down Expand Up @@ -85,25 +86,58 @@ def magnet2torrent(magnet, output_name=None):

return output


def showHelp():
print("")
print("USAGE: " + pt.basename(sys.argv[0]) + " MAGNET [OUTPUT]")
print(" MAGNET\t- the magnet url")
print(" OUTPUT\t- the output torrent file name")
print("")


def main():
if len(sys.argv) < 2:
showHelp()
sys.exit(0)

magnet = sys.argv[1]
parser = ArgumentParser(description="A command line tool that converts magnet links in to .torrent files")
parser.add_argument('-m','--magnet', help='The magnet url')
parser.add_argument('-o','--output', help='The output torrent file name')

#
# This second parser is created to force the user to provide
# the 'output' arg if they provide the 'magnet' arg.
#
# The current version of argparse does not have support
# for conditionally required arguments. That is the reason
# for creating the second parser
#
# Side note: one should look into forking argparse and adding this
# feature.
#
conditionally_required_arg_parser = ArgumentParser(description="A command line tool that converts magnet links in to .torrent files")
conditionally_required_arg_parser.add_argument('-m','--magnet', help='The magnet url')
conditionally_required_arg_parser.add_argument('-o','--output', help='The output torrent file name', required=True)

magnet = None
output_name = None

if len(sys.argv) >= 3:
output_name = sys.argv[2]
#
# Attempting to retrieve args using the new method
#
args = vars(parser.parse_known_args()[0])
if args['magnet'] is not None:
magnet = args['magnet']
argsHack = vars(conditionally_required_arg_parser.parse_known_args()[0])
output_name = argsHack['output']
if args['output'] is not None and output_name is None:
output_name = args['output']
if magnet is None:
#
# This is a special case.
# This is when the user provides only the "output" args.
# We're forcing him to provide the 'magnet' args in the new method
#
print ('usage: {0} [-h] [-m MAGNET] -o OUTPUT'.format(sys.argv[0]))
print ('{0}: error: argument -m/--magnet is required'.format(sys.argv[0]))
sys.exit()
#
# Defaulting to the old of doing things
#
if output_name is None and magnet is None:
if len(sys.argv) >= 2:
magnet = sys.argv[1]
if len(sys.argv) >= 3:
output_name = sys.argv[2]

print magnet, output_name

This comment has been minimized.

Copy link
@parnmatt

parnmatt Feb 28, 2016

Contributor

breaks python3 support


magnet2torrent(magnet, output_name)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ A command line tool that converts magnet links in to .torrent files.
`python Magnet_To_Torrent2.py <magnet link> [torrent file]`

### Example
`python Magnet_To_Torrent2.py "magnet:?xt=urn:btih:49fbd26322960d982da855c54e36df19ad3113b8&dn=ubuntu-12.04-desktop-i386.iso&tr=udp%3A%2F%2Ftracker.openbittorrent.com" ubunut12-04.iso`
`python Magnet_To_Torrent2.py -m "magnet:?xt=urn:btih:49fbd26322960d982da855c54e36df19ad3113b8&dn=ubuntu-12.04-desktop-i386.iso&tr=udp%3A%2F%2Ftracker.openbittorrent.com" -o ubunut12-04.iso`

## Licenses
All code is licensed under the [GPL version 3](http://www.gnu.org/licenses/gpl.html)

0 comments on commit 71da071

Please sign in to comment.