New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'prompt()' to cement2.tools #119

Closed
derks opened this Issue Jun 22, 2012 · 7 comments

Comments

Projects
None yet
2 participants
@derks
Member

derks commented Jun 22, 2012

Requesting input from the user is easy enough, but just prompting for a 'Yes/No' response is pretty common and can be annoying:

from cement2.core import meta
class prompt(meta.MetaMixin):
    class Meta:
        msg = 'Continue'
        display_options = ['Y', 'n']
        valid_options = ['1', 'Y', 'y', 'yes','Yes', 'YES']

    def __init__(self, *args, **kw):
        super(prompt, self).__init__(*args, **kw)
        self.input = None
        self._prompt()

    def _prompt(self):
        if self._meta.display_options is not None:
            msg = "%s [%s] " % (self._meta.msg, 
                                 '/'.join(self._meta.display_options))
        else:
            msg = "%s " % self._meta.msg
        self.input = raw_input(msg).strip('\n')
        return self.input

    def is_valid(self):
        if self._meta.valid_options is None:
            return True
        elif self.input in self._meta.valid_options:
            return True 
        else:
            return False

i = prompt()
print i.is_valid()   
print i.input

@ghost ghost assigned derks Jun 22, 2012

@derks

This comment has been minimized.

Show comment
Hide comment
@derks

derks Jun 22, 2012

Member
$ python test.py
Continue: [Y/n] yes
Valid input
Member

derks commented Jun 22, 2012

$ python test.py
Continue: [Y/n] yes
Valid input
@derks

This comment has been minimized.

Show comment
Hide comment
@derks

derks Jun 22, 2012

Member
i = prompt(
    msg="What is your name?",
    display_options=None,
    valid_options=None,
    )

print "You're name is %s" % i.input
$ python test.py
What is your name? Johnny
You're name is Johnny
Member

derks commented Jun 22, 2012

i = prompt(
    msg="What is your name?",
    display_options=None,
    valid_options=None,
    )

print "You're name is %s" % i.input
$ python test.py
What is your name? Johnny
You're name is Johnny
@derks

This comment has been minimized.

Show comment
Hide comment
@derks

derks Jul 3, 2012

Member

Will revisit this later. Would be nice to do it up right... like an inline prompt:

Continue? [Y/n] y

But also maybe a listed prompt builder like:

Select your city:

1 : San Antonio
2 : Austin
3 : Dallas
4 : Houston

City Selection: [default: 1] 3
Member

derks commented Jul 3, 2012

Will revisit this later. Would be nice to do it up right... like an inline prompt:

Continue? [Y/n] y

But also maybe a listed prompt builder like:

Select your city:

1 : San Antonio
2 : Austin
3 : Dallas
4 : Houston

City Selection: [default: 1] 3

derks added a commit that referenced this issue Aug 8, 2014

derks added a commit that referenced this issue Aug 8, 2014

@derks derks closed this Aug 8, 2014

@spinus

This comment has been minimized.

Show comment
Hide comment
@spinus

spinus Aug 12, 2014

Contributor

I like cement as a cli. Here you are introducing user interactive interface, if cement is cli framework I don't think this should be part of it. If people want they can extend it using urwid library for example or another user friendly input. What do you think?

Contributor

spinus commented Aug 12, 2014

I like cement as a cli. Here you are introducing user interactive interface, if cement is cli framework I don't think this should be part of it. If people want they can extend it using urwid library for example or another user friendly input. What do you think?

@derks

This comment has been minimized.

Show comment
Hide comment
@derks

derks Aug 12, 2014

Member

I agree, it isn't Cement's goal to be a full-function interactive interface, however the code in cement.utils is general helper functionality to limit unnecessary code for common tasks. Basically, taking up a couple lines of code to grab basic input from the user everytime is just tedious to me... why not just have a simple wrapper to make the process easy for most folks that are doing strictly command line stuff and not interested in getting into a full-fledge UI like urwid or similar.

I don't think introducing a prompt wrapper like this is really stepping the bounds of something like urwid, but that's just my opinion. That said, I'd be more than happy to yank it before 2.4.0 stable if more people agreed that it shouldn't exist in Cement.

Member

derks commented Aug 12, 2014

I agree, it isn't Cement's goal to be a full-function interactive interface, however the code in cement.utils is general helper functionality to limit unnecessary code for common tasks. Basically, taking up a couple lines of code to grab basic input from the user everytime is just tedious to me... why not just have a simple wrapper to make the process easy for most folks that are doing strictly command line stuff and not interested in getting into a full-fledge UI like urwid or similar.

I don't think introducing a prompt wrapper like this is really stepping the bounds of something like urwid, but that's just my opinion. That said, I'd be more than happy to yank it before 2.4.0 stable if more people agreed that it shouldn't exist in Cement.

@spinus

This comment has been minimized.

Show comment
Hide comment
@spinus

spinus Aug 12, 2014

Contributor

I don't say it should be removed. I say it's another code, not related too much to framework itself but probably this code have to be maintained and tested anyway. I say that as cli framework maybe those tools are not needed in a core because they are introducing complexity, maybe they fit better as a cement-utils package or something like this? If you want them to maintain in a core that's ok with me.

Contributor

spinus commented Aug 12, 2014

I don't say it should be removed. I say it's another code, not related too much to framework itself but probably this code have to be maintained and tested anyway. I say that as cli framework maybe those tools are not needed in a core because they are introducing complexity, maybe they fit better as a cement-utils package or something like this? If you want them to maintain in a core that's ok with me.

@derks

This comment has been minimized.

Show comment
Hide comment
@derks

derks Aug 12, 2014

Member

Ah, OK... I follow you. Definitely good feedback, and I am very much on board with keeping core as clean as possible. I don't think this blurs the lines too much.. I sort of view it similar to an "abort(404)" wrapper in a web framework, where the pieces to handle a HTTP 404 are all there already, but wrapping it in a simple call like that ends up being (a little bit) less hassle for the developer.

In the past I've attempted to maintain all extensions externally from Cement code base itself, and it was just a nightmare to manage especially when it came to code compatibility, and testing. If this bit of code was more complex, or if I was concerned about maintainability, it would definitely go into an optional extension or a completely external one (which is always possible to do outside of Cement).

Member

derks commented Aug 12, 2014

Ah, OK... I follow you. Definitely good feedback, and I am very much on board with keeping core as clean as possible. I don't think this blurs the lines too much.. I sort of view it similar to an "abort(404)" wrapper in a web framework, where the pieces to handle a HTTP 404 are all there already, but wrapping it in a simple call like that ends up being (a little bit) less hassle for the developer.

In the past I've attempted to maintain all extensions externally from Cement code base itself, and it was just a nightmare to manage especially when it came to code compatibility, and testing. If this bit of code was more complex, or if I was concerned about maintainability, it would definitely go into an optional extension or a completely external one (which is always possible to do outside of Cement).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment